c++ - 从拆分字符串定义变量?

标签 c++

所以,我编写了这段代码,它基本上将用户输入拆分为不同的字符串。

例如

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/

相关文章:

c++ - 哪些语言会在没有显式桥接的情况下调用 C++?

c++ - 返回一个空 vector c++

c++ - 如何实现 "InterpolatedVector"?

c++ - 链接错误与 fmt : undefined reference to `std::string fmt::v6::internal::grouping_impl<char>(fmt::v6::internal::locale_ref)'

c++ - 是否有相当于这个 "safe_read"调用的 boost

c++ - 如何调用 DeviceIoControl 来检索它需要的内存量?

c++ - 使用 3D 数组

c++ - 使用 g++ 4.8 时缺少 std::vector::erase() 的 const_iterator 重载

c++ - 字符串转 float ,C++实现

c++ - 在两个 Jenkins 构建中的文件之间运行特定的 diff 命令