更新时间:2023-02-18 19:32:26
将子字符串放在向量中。这里有一个例子:
std :: string str;
std :: cin>> str;
std :: size_t pos = 0,tmp;
std :: vector< std :: string>值;
while((tmp = str.find('。',pos))!= std :: string :: npos){
values.push_back(str.substr(pos,tmp - pos)) ;
pos = tmp + 1;
}
values.push_back(str.substr(pos,std :: string :: npos));
for(pos = 0; pos< values.length(); ++ pos)
{
std :: cout< 字符串部分<< pos }
So, I've made this code, and it basically splits up the users input into different strings.
For example
Workspace.Hello.Hey would then be printed out as "Workspace" "Hello" "Hey"
However, I need to know how to define each of those as their own SEPARATE variable that can be called later on. This is the code I have.
std::string str;
std::cin >> str;
std::size_t pos = 0, tmp;
while ((tmp = str.find('.', pos)) != std::string::npos) {
str[tmp] = '\0';
std::cout << "Getting " << str.substr(pos) << " then ";
pos = tmp;
}
std::cout << "Getting " << str.substr(pos) << " then ";
Put the substrings in a vector. Here's an example:
std::string str;
std::cin >> str;
std::size_t pos = 0, tmp;
std::vector<std::string> values;
while ((tmp = str.find('.', pos)) != std::string::npos) {
values.push_back(str.substr(pos, tmp - pos));
pos = tmp + 1;
}
values.push_back(str.substr(pos, std::string::npos));
for (pos = 0; pos < values.length(); ++pos)
{
std::cout << "String part " << pos << " is " << values[pos] << std::endl;
}