使用C++; 我试图从C++中的字符串中提取整数序列,其中包含一个特定的定界符,并用它们创建数组:
输入的格式如下:使用C++; 我试图从C++中的字符串中提取整数序列,其中包含一个特定的定界符,并用它们创建数组:,c++,arrays,string,C++,Arrays,String,输入的格式如下: ()+(()+)+ 例如:1234;5 6 7 8; 9 10(此处分隔符为;) 结果应为三个整数数组,包含: [1,2,3,4],[5,6,7,8]和[9,10] 到目前为止,我尝试使用的是istringstream,因为它已经用空格分隔了它们,但我没有成功: #include <iostream> #include <sstream> using namespace std; int main() { string token;
()+(()+)+
例如:1234;5 6 7 8; 9 10
(此处分隔符为;
)
结果应为三个整数数组,包含:
[1,2,3,4]
,[5,6,7,8]
和[9,10]
到目前为止,我尝试使用的是
istringstream
,因为它已经用空格分隔了它们,但我没有成功:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string token;
cin >> token;
istringstream in(token);
// Here is the part that is confusing me
// Also, I don't know how the size of the newly created array
// will be determined
if (!in.fail()) {
cout << "Success!" << endl;
} else {
cout << "Failed: " << in.str() << endl;
}
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
字符串标记;
cin>>令牌;
istringstream in(令牌);
//这是让我困惑的部分
//另外,我不知道新创建的数组的大小如何
//将决定
如果(!in.fail()){
cout我的建议是使用std::getline
读取;”
,然后使用std::istringstream
解析字符串:
std::string tokens;
std::getline(cin, tokens, ';');
std::istringstream token_stream(tokens);
std::vector<int> numbers;
int value;
while (token_stream >> value)
{
numbers.push_back(value);
}
std::字符串标记;
std::getline(cin,tokens,“;”);
std::istringstream令牌流(令牌);
std::向量数;
int值;
while(令牌流>>值)
{
数字。推回(值);
}
我的建议是使用std::getline
读取;”
,然后使用std::istringstream
解析字符串:
std::string tokens;
std::getline(cin, tokens, ';');
std::istringstream token_stream(tokens);
std::vector<int> numbers;
int value;
while (token_stream >> value)
{
numbers.push_back(value);
}
std::字符串标记;
std::getline(cin,tokens,“;”);
std::istringstream令牌流(令牌);
std::向量数;
int值;
while(令牌流>>值)
{
数字。推回(值);
}
要执行前面的答案,请使用std::getline
读取;”
,然后使用std::istringstream
解析字符串,直至其空格:
std::string tokens;
std::getline(cin, tokens);
std::istringstream token_stream(tokens);
std::vector<string> arr;
vector<vector<int>> toReturn
string cell;
while (getline(token_stream, cell, ';')
{
arr.push_back(cell);
}
for(int i = 0; i<arr.size(); i++)
{
istringstream n(arr[i]);
vector<int> temp;
while(getline(n, cell, ' ') temp.push_back(atof(cell));
toReturn.push_back(temp);
}
std::字符串标记;
std::getline(cin,令牌);
std::istringstream令牌流(令牌);
std::载体arr;
向量回归
字符串单元;
while(getline(令牌流,单元格“;”)
{
arr.push_back(单元);
}
对于(int i=0;i来说,执行前面的答案是使用std::getline
读取;”
,然后使用std::istringstream
解析字符串,直到其空格:
std::string tokens;
std::getline(cin, tokens);
std::istringstream token_stream(tokens);
std::vector<string> arr;
vector<vector<int>> toReturn
string cell;
while (getline(token_stream, cell, ';')
{
arr.push_back(cell);
}
for(int i = 0; i<arr.size(); i++)
{
istringstream n(arr[i]);
vector<int> temp;
while(getline(n, cell, ' ') temp.push_back(atof(cell));
toReturn.push_back(temp);
}
std::字符串标记;
std::getline(cin,令牌);
std::istringstream令牌流(令牌);
std::载体arr;
向量回归
字符串单元;
while(getline(令牌流,单元格“;”)
{
arr.push_back(单元);
}
对于(int i=0;i