所以,我编写了这段代码,它基本上将用户输入拆分为不同的字符串。
例如
Workspace.Hello.Hey would then be printed out as "Workspace" "Hello" "Hey"
但是,我需要知道如何将它们中的每一个定义为它们自己的单独变量,以便稍后调用。 这是我的代码。
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 ";
最佳答案
C++ 有一个 vector 对象,您可以在其中将它们存储在连续的索引中并根据需要访问它们。
再想想你在做什么,将字符串输入字符串流可能更容易,set 。作为分隔符,然后将内容读入如上的字符串 vector 。
关于c++ - 从拆分字符串定义变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34487014/