从分割字符串定义变量? [英] Defining variables from split strings?
本文介绍了从分割字符串定义变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如
$ b $ b
Workspace.Hello.Hey将打印为WorkspaceHelloHey
但是,我需要知道如何定义它们作为自己的SEPARATE变量,稍后可以调用。
这是我有的代码。
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<< 获得<< str.substr(pos)< 然后 ;
pos = tmp;
}
std :: cout<< 获得<< str.substr(pos)< 然后 ;
解决方案
将子字符串放在向量中。这里有一个例子:
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 < is<<值[pos] std :: endl;
}
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;
}
这篇关于从分割字符串定义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文