size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
std::cout << token << std::endl;
s.erase(0, pos + delimiter.length());
}
static const size_t npos = -1;
Maximum value for size_t
npos is a static member constant value with the greatest possible value for an element of type size_t.
This value, when used as the value for a len (or sublen) parameter in string's member functions, means "until the end of the string".
As a return value, it is usually used to indicate no matches.
This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type.
input: 1 2 3 4
vector<int> a;
string line;
getline(cin, line);
size_t pos = 0;
string token;
string delimiter = " ";
while ((pos = line.find(delimiter))!=std::string::npos) {
token = line.substr(0, pos);
a.push_back(atoi(token.c_str()));
line.erase(0, pos + delimiter.length());
}
a.push_back(atoi(line.c_str()));
No comments:
Post a Comment