我需要将字符串解析为具有不同数据类型(ints,strings)的变量。
所讨论的字符串是从文件中的一行中获取的。
我想知道是否存在类似于Infile>> var1>> var2>>等的函数;我可以用于字符串。以下是文件中的完整行。
2016/12/6 S"黑暗中政府与大业务之间的乱伦关系。
我已经分配了" 2016/12/6"," S",以及使用Infile>>的变量之间的引号与变量之间的所有内容。另外,在最后出现双引号之后,我将所有内容都拿走了,并将其存储在字符串retofline中。现在,我想将RESTOFLINE分析为每个值的变量(0、3、39,蓝色,白色,帕特里克,Bardwell,pat.bardwell@bwpmlp.com都应该是单独的变量)。我可以使用这样的方法吗?我还尝试使用RESTOFLINE.find()和RESTOFLINE.SUBSTR()将它们分开,但无法弄清楚它们。同样,如果我可以比当前代码更有效地将每个值与整个行分开,我更喜欢。下面的当前代码。非常感谢任何帮助。
int main()
{
// Declare variables
string userFile;
string line;
string date;
char printMethod;
string message;
int numMedium;
int numLarge;
int numXL;
string shirtColor;
string inkColor;
string firstName;
string lastName;
string customerEmail;
string firstLine;
string restOfLine;
// Prompt user to 'upload' file
cout << "Please input the name of your file:n";
cin >> userFile;
fstream inFile;
inFile.open(userFile.c_str());
// Check if file open successful -- if so, process
if (inFile.is_open())
{
getline(inFile, firstLine); // get column headings out of the way
cout << firstLine << endl << endl;
while(inFile.good())
// while we are not at the end of the file, process
{
getline(inFile, line);
inFile >> date >> printMethod; // assigns first two values of line to date and printMethod, respectively
int pos1 = line.find(""");
// find first occurrence of a double quotation mark and assign position value to pos1
int pos2 = line.rfind(""");
// find last occurrence of a double quotation mark and assign position value to pos2
string message = line.substr(pos1, (pos2 - pos1));
// sets message to string between quotation marks
string restOfLine = line.substr(pos2 + 2);
// restOfLine = everything after the message -- used to parse
}
inFile.close();
}
// If file open failure, output error message, exit with return 0;
else
{
cout << "Error opening file";
}
return 0;
}
我想知道是否存在类似于Infile>> var1>> var2>>等的函数;我可以用于字符串?
是的,而不仅仅是相似,实际上相同。字符串流与文件流相同。
std::stringstream ss(restOfLine);
ss >> numMedium >> numLarge >> numXL >> shirtColor >> inkColor >> firstName >> lastName >> customerEmail >> firstLine;
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
unsigned int split(const std::string &txt, std::vector<std::string> &strs, char ch);
int main(int argc, const char * argv[]) {
string text = "2016/12/6 s "The incestuous relationship between government and big business thrives in the dark. ~Jack Anderson [4]" 0 3 39 blue white PATRICK BARDWELL pat.bardwell@bwpmlp.com ";
std::vector<std::string> v;
split( text, v, ' ' );
return 0;
}
unsigned int split(const std::string &txt, std::vector<std::string> &strs, char ch)
{
unsigned int pos = static_cast<unsigned int>(txt.find( ch ));
unsigned int initialPos = 0;
strs.clear();
// Decompose statement
while( pos >! txt.size()) {
strs.push_back( txt.substr( initialPos, pos - initialPos + 1 ) );
initialPos = pos + 1;
pos = static_cast<unsigned int>(txt.find( ch, initialPos));
if(pos > txt.size()) break;
}
// Add the last one
// strs.push_back( txt.substr( initialPos, std::min( pos, static_cast<unsigned int>(txt.size() )) - initialPos + 1 ) );
return static_cast<unsigned int>(strs.size());
}
上面的程序将使您的字符串分解为零件,然后使用下面提到的功能进行数据类型转换。要将字符串转换为int,您可以使用std :: stoi(str)这在C 11中可用。所有类型的数字都有版本:长stol(string),float stof(string),double stod(string),...有关更多信息