E
electrixnow
Can anyone tell me who sizeof &tokens only return a int of 4 no matter
how many
strings that are in the token[].
thanks,
Grant
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void Tokenize(const string& str,
vector<string>& tokens,
const string& delimiters = " ,\t\n")
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
int main()
{
vector<string> tokens;
string str(";1,2,3,4,5,6,7,8,9,0"); // replace with getline
string start = str.substr(0,1);
if(start == ";")cout<<"HELLO WORLD"<<'\n'; // skip this line for
comments
int len = str.length();
for (int a=0; a<len; ++a)
str[a]=toupper(str[a]);
Tokenize(str, tokens);
for (int i=0;i<sizeof(&tokens);++i){ // check amount of tokens
cout << tokens<<'\n';
}
}
how many
strings that are in the token[].
thanks,
Grant
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void Tokenize(const string& str,
vector<string>& tokens,
const string& delimiters = " ,\t\n")
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
int main()
{
vector<string> tokens;
string str(";1,2,3,4,5,6,7,8,9,0"); // replace with getline
string start = str.substr(0,1);
if(start == ";")cout<<"HELLO WORLD"<<'\n'; // skip this line for
comments
int len = str.length();
for (int a=0; a<len; ++a)
str[a]=toupper(str[a]);
Tokenize(str, tokens);
for (int i=0;i<sizeof(&tokens);++i){ // check amount of tokens
cout << tokens<<'\n';
}
}