C++ 如何将字符串转换为向量
我正在写两个程序,第一个程序有一个整数数组C++ 如何将字符串转换为向量,c++,string,vector,converter,C++,String,Vector,Converter,我正在写两个程序,第一个程序有一个整数数组 vector<int> v = {10, 200, 3000, 40000}; 第二个程序将读取文件,将字符串转换回整数,然后将其推回矢量 守则: string stringword; ifstream myfile; myfile.open ("writtentext"); getline (myfile,stringword); cout << "Read From File = " << stringwor
vector<int> v = {10, 200, 3000, 40000};
第二个程序将读取文件,将字符串转换回整数,然后将其推回矢量
守则:
string stringword;
ifstream myfile;
myfile.open ("writtentext");
getline (myfile,stringword);
cout << "Read From File = " << stringword << endl;
cout << "Convert back to vector = " ;
for (int i=0;i<stringword.length();i++)
{
if (stringword.find(','))
{
int value;
istringstream (stringword) >> value;
v.push_back(value);
stringword.erase(0, stringword.find(','));
}
}
for (int j=0;j<v.size();j++)
{
cout << v.at(j) << " " ;
}
我做错了什么?感谢(int j=0;jfor循环出现问题
for (int j=0;j<v.size();i++)
{
cout << v.at(i) << " " ;
}
考虑这一点:
while(1) //Use a while loop, "i" isn't doing anything for you
{
//if comman not found find return string::npos
if (stringword.find(',')!=std::string::npos)
{
int value;
istringstream (stringword) >> value;
v.push_back(value);
//Erase all element including comma
stringword.erase(0, stringword.find(',')+1);
}
else
break; //Come out of loop
}
相反,只需使用std::stringstream
从文件读回即可
std::stringstream ss(stringword);
int value;
while (ss >> value)
{
v.push_back(value);
if (ss.peek() == ',')
ss.ignore();
}
for (int j=0;j<v.size();j++) //Fix variables
{
cout << v.at(j) << " " ; // Can use simply v[j]
}
std::stringstream ss(stringword);
int值;
while(ss>>值)
{
v、 推回(值);
如果(ss.peek()==',')
忽略();
}
对于(int j=0;j您可以跳过字符串转换。所有流都可以处理int
类型
std::vector
输出:
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
std::ostream_iterator<int> output_iterator(std::cout, ",");
std::copy(v.begin(), v.end(), output_iterator);
}
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
int main() {
std::vector<int> v;
int value;
std::string line;
while(getline(cin, line, ',')) {
std::stringstream ss(line);
ss >> value
v.push_back(value);
}
typedef std::vector<int>::iterator iter;
iter end = v.end();
for(iter it = v.begin(); it != end; ++it) {
std::cout << *it << endl;
}
}
#包括
#包括
#包括
int main(){
std::向量v;
v、 推回(1);
v、 推回(2);
v、 推回(3);
v、 推回(4);
std::ostream_迭代器输出_迭代器(std::cout,“,”);
复制(v.begin(),v.end(),输出迭代器);
}
输入到std::vector
:
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
std::ostream_iterator<int> output_iterator(std::cout, ",");
std::copy(v.begin(), v.end(), output_iterator);
}
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
int main() {
std::vector<int> v;
int value;
std::string line;
while(getline(cin, line, ',')) {
std::stringstream ss(line);
ss >> value
v.push_back(value);
}
typedef std::vector<int>::iterator iter;
iter end = v.end();
for(iter it = v.begin(); it != end; ++it) {
std::cout << *it << endl;
}
}
#包括
#包括
#包括
使用名称空间std;
int main(){
std::向量v;
int值;
std::字符串行;
while(getline(cin,line,,')){
std::stringstream ss(线路);
ss>>值
v、 推回(值);
}
typedef std::vector::iterator iter;
iter端=v.端();
for(iter it=v.begin();it!=end;++it){
这只是你犯的一个错误:
for (int j=0;j<v.size();i++)
{
cout << v.at(i) << " " ;
}
for(int j=0;j您可以简化程序:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
// output
int main()
{
// my compiler doesn't support initializer lists yet :(
std::vector<int> v(4);
v[0] = 10;
v[1] = 200;
v[2] = 3000;
v[3] = 40000;
std::ofstream fout("mytestfile.txt");
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(fout, ","));
fout.close();
return 0;
}
// input
struct int_reader : std::ctype<char>
{
int_reader() : std::ctype<char>(get_table()) {}
static std::ctype_base::mask const* get_table()
{
static std::vector<std::ctype_base::mask> rc(table_size, std::ctype_base::mask());
rc[','] = std::ctype_base::space;
rc['\n'] = std::ctype_base::space;
return &rc[0];
}
};
int main()
{
std::vector<int> v;
std::ifstream fin("mytestfile.txt", std::ifstream::in);
fin.imbue(std::locale(std::locale(), new int_reader()));
std::copy(std::istream_iterator<int>(fin), std::istream_iterator<int>(), std::back_inserter<std::vector<int>>(v));
fin.close();
std::cout << "You read in: ";
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
return 0;
}
#包括
#包括
#包括
#包括
#包括
//输出
int main()
{
//我的编译器尚不支持初始值设定项列表:(
std::向量v(4);
v[0]=10;
v[1]=200;
v[2]=3000;
v[3]=40000;
std::of stream fout(“mytestfile.txt”);
std::copy(v.begin(),v.end(),std::ostream_迭代器(fout,“,”);
fout.close();
返回0;
}
//输入
结构int_读取器:std::ctype
{
int_reader():std::ctype(get_table()){}
静态std::ctype_base::mask const*get_table()
{
静态std::vector rc(表大小,std::ctype_base::mask());
rc[',']=std::ctype_base::space;
rc['\n']=std::ctype_base::space;
返回&rc[0];
}
};
int main()
{
std::向量v;
std::ifstream fin(“mytestfile.txt”,std::ifstream::in);
fin.imbue(std::locale(std::locale(),new int_reader());
std::copy(std::istream_迭代器(fin),std::istream_迭代器(),std::back_插入器(v));
fin.close();
std::cout虽然这是手工制作的,而不是使用std::stringstream
,但我发现它很容易理解
std::vector<std::string> StringToVector (const std::string s,
const char token)
{
std::vector<std::string> v;
size_t posLast = 0, pos = 0;
while((pos = s.find(token, pos)) != std::string::npos)
{
if(s[pos] != s[posLast])
v.push_back(s.substr(posLast, pos - posLast));
posLast = ++pos;
}
if(s[posLast] != 0) // If there is no terminating token found
v.push_back(s.substr(posLast));
return v;
}
std::vector StringToVector(const std::string s,
常量字符(令牌)
{
std::向量v;
大小\u t posLast=0,pos=0;
while((pos=s.find(token,pos))!=std::string::npos)
{
如果(s[pos]!=s[posLast])
v、 向后推(s.substr(posLast,pos-posLast));
posLast=++pos;
}
if(s[posLast]!=0)//如果未找到终止令牌
v、 推回(s.substr(posLast));
返回v;
}
使用各种测试用例。跳过字符串部分,只需将向量写入文件。检查stringword.find(',')
返回的内容()。如果这不明显,请通过调试器运行代码,您将看到。在执行此操作时,还可以检查文档中的擦除
:。正确,但这不是问题所在
for (int j=0;j<v.size();i++)
{
cout << v.at(i) << " " ;
}
for (int i=0;i<stringword.length();i++)
{
if (stringword.find(','))
{
int value;
istringstream (stringword) >> value;
v.push_back(value);
stringword.erase(0, stringword.find(','));
}
}
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
int ConvertStringToInt(const std::string &str, std::vector<int> &ints)
{
int count_int = 0;
std::string string_int;
size_t start = 0;
size_t end = 0;
while ((end = str.find(',', start)) != std::string::npos)
{
string_int.assign(str, start, end - start);
ints.push_back(atoi(string_int.c_str()));
start = end + 1;
++count_int;
}
if (start != str.size())
{
ints.push_back(atoi(str.c_str() + start));
++count_int;
}
return count_int;
}
int main(int argc, char *const argv[])
{
std::vector<int> ints;
std::string str = "123,456,789 ";
std::cout << ConvertStringToInt(str, ints) << std::endl;
for (size_t i = 0; i != ints.size(); ++i)
{
std::cout << ints[i] << std::endl;
}
return 0;
}
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
// output
int main()
{
// my compiler doesn't support initializer lists yet :(
std::vector<int> v(4);
v[0] = 10;
v[1] = 200;
v[2] = 3000;
v[3] = 40000;
std::ofstream fout("mytestfile.txt");
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(fout, ","));
fout.close();
return 0;
}
// input
struct int_reader : std::ctype<char>
{
int_reader() : std::ctype<char>(get_table()) {}
static std::ctype_base::mask const* get_table()
{
static std::vector<std::ctype_base::mask> rc(table_size, std::ctype_base::mask());
rc[','] = std::ctype_base::space;
rc['\n'] = std::ctype_base::space;
return &rc[0];
}
};
int main()
{
std::vector<int> v;
std::ifstream fin("mytestfile.txt", std::ifstream::in);
fin.imbue(std::locale(std::locale(), new int_reader()));
std::copy(std::istream_iterator<int>(fin), std::istream_iterator<int>(), std::back_inserter<std::vector<int>>(v));
fin.close();
std::cout << "You read in: ";
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
return 0;
}
std::vector<std::string> StringToVector (const std::string s,
const char token)
{
std::vector<std::string> v;
size_t posLast = 0, pos = 0;
while((pos = s.find(token, pos)) != std::string::npos)
{
if(s[pos] != s[posLast])
v.push_back(s.substr(posLast, pos - posLast));
posLast = ++pos;
}
if(s[posLast] != 0) // If there is no terminating token found
v.push_back(s.substr(posLast));
return v;
}