C++ 读取控制台字符串并按 '-' 分割
以下是示例代码:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
int main()
{
std::string input;
std::getline(std::cin, input); // 读取控制台一行字符串
std::istringstream iss(input);
std::vector<std::string> dd; // 存储分割后的字符的数组
std::string token;
while (std::getline(iss, token, '-')) // 以'-'为分隔符分割字符串
{
dd.push_back(token); // 将分割后的字符存入数组
}
// 输出分割后的字符
for (const auto& s : dd)
{
std::cout << s << std::endl;
}
return 0;
}
运行示例:
输入:hello-world-cpp
输出:
hello
world
cpp
原文地址: https://www.cveoy.top/t/topic/owKo 著作权归作者所有。请勿转载和采集!