如何从一行 C++ 中提取两个字符串
how to extract two strings from a line c++
假设我必须读取以下输入:
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
str1 str2
atcay
ittenkay
oopslay
所以我无法单独存储所有字符串。 这是我能想出的一部分代码。
while(1)
{
getline(cin,s);
if(s.empty())
break;
else
cout<<s<<endl;
}
所以现在我可以将"dog ogday"存储在一个字符串中。但我想将它们存储在单独的字符串中。请帮忙。(提前致谢:D(
使用cin
获取两个字符串:
string a,b;
cin >> a >> b;
int i=0;
while(i<9){
getline(cin,s);
if(s.empty())
break;
else
cout<<s<<endl;
i++;
}
不确定这是否是您想要的,但我认为它会让您存储所有 9 个字符串。
这将分别读取所有"单词"(用空格分隔(。
#include <iostream>
#include <string>
#include <sstream>
int main()
{
for (std::string s; std::getline(std::cin, s) && !(s.empty() || s.find_first_not_of(' ') == std::string::npos); )
{
std::istringstream stream{s};
for (std::string str; stream >> str; std::cout << str << 'n');
}
}
相关文章:
- 从字符串中提取整数并形成一个数组
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 如何在字符串中找到字符,然后在C++中提取其余的字符串
- std::regex:匹配由数字和空格组成的字符串,并提取数字.如何?
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 清除输入缓冲区后未提取字符串流
- 如何创建一个函数来提取向量内部字符串中的字段?
- 从没有空格的字符串中提取整数
- 如何使用正则表达式从字符串中提取值
- 使用正则表达式c++从单词和分隔符之间的字符串中提取所有子字符串
- 提取字符串中的随机单词
- 将子字符串提取到最后 n 个分隔符C++
- 如何在C 中不带有弦乐和strtok的情况下拆分字符串(提取单词)
- Arduino中的字符串提取问题
- C++ 在将字符串提取到标量时处理溢出/下溢
- 将两个 '' 字符之间的子字符串提取到单独的数组中
- 字符提取与字符串提取有何不同
- 如何从输入字符串提取数值?c++
- 作为XML字符串提取请求/响应