C++ - 将用逗号分隔的用户输入字符串存储到向量中
C++ - Storing user input string separated by commas into vector
我编写了一段代码,可以在一定程度上执行此任务。但是,我想知道如何更改我的代码,以便我可以存储用户想要输入到向量中的尽可能多的字符串输入。
这是我的代码:
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int main ()
{
string input = "";
cout << "Input: ";
cin >> input;
string a,b;
for(int i = 0; i<input.size(); i++)
{
if(input.at(i)==','){
a=input.substr(0,i);
b=input.substr(i+1);
}
}
vector<string> objects;
objects.push_back(a);
objects.push_back(b);
for (int k = 0; k < 2; k++) {
cout << objects[k] << endl;
}
return 0;
}
到目前为止,它只能识别和存储两个以逗号分隔的输入。我对编码还很陌生,所以有人可以告诉我一种将其变成循环并接收用户输入的输入的方法吗?
谢谢。
您需要更改代码才能处理任意数量的用户输入。
逻辑是将逗号之间的每个子字符串推入 vector
.
vector<string> objects;
for(int i = 0,j=0; i<input.size(); i++)
{
if(input.at(i)==',' || input.at(i)=='[=10=]'){
objects.push_back(input.substr(j,i-j)); //pushing the sub string
j=i+1;
}
}
为了打印矢量,首先你必须找到矢量的大小,然后简单地迭代打印它。
//display
int l=objects.size();
for (int k = 0; k < l; k++) {
cout << objects[k] << endl;
}
注意: 如果您希望您的代码适用于中间有空格的字符串,例如:a ,b ,c ,d
然后使用 getline(cin,input);
从用户。
可以看到running code here or as a github gist.
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <string>
void ParseCSV(
std::vector< std::string >& output,
const std::string& csv )
{
int q = 0;
int p = csv.find(",");
while( p != -1 )
{
output.push_back( csv.substr(q,p-q) );
q = p+2;
p = csv.find(",",q);
}
// The terminating comma of the CSV is missing
// so we need to check if there is
// one more value to be appended
p = csv.find_last_of(",");
if( p != -1 )
{
output.push_back( csv.substr( p+2 ) );
}
else
{
// there was no comma
// this could be because the list is empty
// it could also be because there is just one element in the list
if( csv.length() > 1 )
output.push_back( csv );
}
}
int main()
{
std::string test("this is my list, a, b, c, d, end of line");
std::vector< std::string > split;
ParseCSV( split, test );
for( auto& s : split )
std::cout << s << std::endl;
}
正如 Christophe 所建议的那样,使用 stringstream 会好得多。无需特殊情况处理!我使用 while 循环 - 似乎更清楚发生了什么。
void ParseCSV2(
std::vector< std::string >& output,
const std::string& csv )
{
std::stringstream sst(csv);
std::string a;
while( getline( sst, a, ',' ) )
output.push_back(a);
}
使用字符串流解析输入字符串有更简单的方法:
string a;
vector<string> objects;
for(stringstream sst(input); getline(sst, a, ','); ) // that's all !
objects.push_back(a);
copy (objects.begin(), objects.end(), ostream_iterator<string>(cout," ; ")); // display all
我编写了一段代码,可以在一定程度上执行此任务。但是,我想知道如何更改我的代码,以便我可以存储用户想要输入到向量中的尽可能多的字符串输入。
这是我的代码:
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int main ()
{
string input = "";
cout << "Input: ";
cin >> input;
string a,b;
for(int i = 0; i<input.size(); i++)
{
if(input.at(i)==','){
a=input.substr(0,i);
b=input.substr(i+1);
}
}
vector<string> objects;
objects.push_back(a);
objects.push_back(b);
for (int k = 0; k < 2; k++) {
cout << objects[k] << endl;
}
return 0;
}
到目前为止,它只能识别和存储两个以逗号分隔的输入。我对编码还很陌生,所以有人可以告诉我一种将其变成循环并接收用户输入的输入的方法吗?
谢谢。
您需要更改代码才能处理任意数量的用户输入。
逻辑是将逗号之间的每个子字符串推入 vector
.
vector<string> objects;
for(int i = 0,j=0; i<input.size(); i++)
{
if(input.at(i)==',' || input.at(i)=='[=10=]'){
objects.push_back(input.substr(j,i-j)); //pushing the sub string
j=i+1;
}
}
为了打印矢量,首先你必须找到矢量的大小,然后简单地迭代打印它。
//display
int l=objects.size();
for (int k = 0; k < l; k++) {
cout << objects[k] << endl;
}
注意: 如果您希望您的代码适用于中间有空格的字符串,例如:a ,b ,c ,d
然后使用 getline(cin,input);
从用户。
可以看到running code here or as a github gist.
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <string>
void ParseCSV(
std::vector< std::string >& output,
const std::string& csv )
{
int q = 0;
int p = csv.find(",");
while( p != -1 )
{
output.push_back( csv.substr(q,p-q) );
q = p+2;
p = csv.find(",",q);
}
// The terminating comma of the CSV is missing
// so we need to check if there is
// one more value to be appended
p = csv.find_last_of(",");
if( p != -1 )
{
output.push_back( csv.substr( p+2 ) );
}
else
{
// there was no comma
// this could be because the list is empty
// it could also be because there is just one element in the list
if( csv.length() > 1 )
output.push_back( csv );
}
}
int main()
{
std::string test("this is my list, a, b, c, d, end of line");
std::vector< std::string > split;
ParseCSV( split, test );
for( auto& s : split )
std::cout << s << std::endl;
}
正如 Christophe 所建议的那样,使用 stringstream 会好得多。无需特殊情况处理!我使用 while 循环 - 似乎更清楚发生了什么。
void ParseCSV2(
std::vector< std::string >& output,
const std::string& csv )
{
std::stringstream sst(csv);
std::string a;
while( getline( sst, a, ',' ) )
output.push_back(a);
}
使用字符串流解析输入字符串有更简单的方法:
string a;
vector<string> objects;
for(stringstream sst(input); getline(sst, a, ','); ) // that's all !
objects.push_back(a);
copy (objects.begin(), objects.end(), ostream_iterator<string>(cout," ; ")); // display all