有没有办法让 C++ 从 cin 中接收未定义数量的字符串?
Is there a way to make C++ take in an undefined number of strings from cin?
我试图让用户输入适当数量的单词(大约 10-20 个)然后解析输入,但是使用下面的代码将等待用户输入 [=17= 的值]每个个字符串。
有没有办法让 C++ 自动用空字符或类似的东西填充剩余的字符串,这样输入的单词数量少于最大值就不会导致阻塞?
代码:
#include <iostream>
#include <string>
int main()
{
std::string test1;
std::string test2;
std::string test3;
std::cout << "Enter values\n:";
std::cin >> test1 >> test2 >> test3;
std::cout << "test1: " << test1 << " test2: " << test2 << " test3: " << test3 << std::endl;
}
你可以使用 while 循环。像这样
string s;
while (cin >> s) {
cout << s << endl;
}
或者获取一个字符串向量并进行 while ,在 while 循环中获取输入并将它们推入向量。
如您所见,它不会存储。如果你想存储。
做
vector<string>text;
while(cin>>s){
text.push_back(s);}
要读取(和存储)未知数量的空白分隔字符串,您需要存储每个字符串。以灵活的方式提供存储的最基本方法是使用 字符串向量 来无限添加(直到您的可用内存限制)。字符串为每个字符串提供存储,向量容器提供了一种将任意数量的字符串收集在一起的简单方法。
您的字符串向量 (vs
) 可以声明为:
#include <iostream>
#include <string>
#include <vector>
...
std::vector<std::string>vs {};
std::vector 提供 .push_back()
成员函数来向向量添加一个元素(在本例中为 string
),例如
std::vector<std::string>vs {};
std::string s;
while (std::cin >> s)
vs.push_back(s);
它简单地读取字符串 s
直到遇到 EOF
,并且使用 vs.push_back(s);
.
将每个读取的字符串添加到字符串向量中
总而言之,你可以做到:
#include <iostream>
#include <string>
#include <vector>
int main (void) {
std::vector<std::string>vs {};
std::string s;
while (std::cin >> s) /* read each string into s */
vs.push_back(s); /* add s to vector of strings */
for (auto& w : vs) /* output each word using range-based loop */
std::cout << w << "\n";
}
例子Use/Output
$ echo "my dog has fleas" | ./bin/readcintostrings
my
dog
has
fleas
检查一下,如果您还有其他问题,请告诉我。
我用这段代码弄明白了:
#include <iostream>
#include <string>
int main()
{
std::string testTemp;
std::string brokenUp[20];
int lastOne = 0;
std::cout << "Enter values\n:";
std::getline(std::cin, testTemp);
for(int current = 0; !testTemp.empty() && testTemp.find(' ') != -1; current++)
{
brokenUp[current] = testTemp.substr(0, testTemp.find(' '));
testTemp.erase(0, testTemp.find(' ') + 1);
lastOne = current;
}
lastOne++;
brokenUp[lastOne] = testTemp;
//this next part is just a test
for(int i = 0; i <= lastOne; i++)
{
std::cout << brokenUp[i] << std::endl;
}
}
但您可以使用任何东西作为分解字符串的存储(即列表或动态数组)。
我试图让用户输入适当数量的单词(大约 10-20 个)然后解析输入,但是使用下面的代码将等待用户输入 [=17= 的值]每个个字符串。
有没有办法让 C++ 自动用空字符或类似的东西填充剩余的字符串,这样输入的单词数量少于最大值就不会导致阻塞?
代码:
#include <iostream>
#include <string>
int main()
{
std::string test1;
std::string test2;
std::string test3;
std::cout << "Enter values\n:";
std::cin >> test1 >> test2 >> test3;
std::cout << "test1: " << test1 << " test2: " << test2 << " test3: " << test3 << std::endl;
}
你可以使用 while 循环。像这样
string s;
while (cin >> s) {
cout << s << endl;
}
或者获取一个字符串向量并进行 while ,在 while 循环中获取输入并将它们推入向量。
如您所见,它不会存储。如果你想存储。 做
vector<string>text;
while(cin>>s){
text.push_back(s);}
要读取(和存储)未知数量的空白分隔字符串,您需要存储每个字符串。以灵活的方式提供存储的最基本方法是使用 字符串向量 来无限添加(直到您的可用内存限制)。字符串为每个字符串提供存储,向量容器提供了一种将任意数量的字符串收集在一起的简单方法。
您的字符串向量 (vs
) 可以声明为:
#include <iostream>
#include <string>
#include <vector>
...
std::vector<std::string>vs {};
std::vector 提供 .push_back()
成员函数来向向量添加一个元素(在本例中为 string
),例如
std::vector<std::string>vs {};
std::string s;
while (std::cin >> s)
vs.push_back(s);
它简单地读取字符串 s
直到遇到 EOF
,并且使用 vs.push_back(s);
.
总而言之,你可以做到:
#include <iostream>
#include <string>
#include <vector>
int main (void) {
std::vector<std::string>vs {};
std::string s;
while (std::cin >> s) /* read each string into s */
vs.push_back(s); /* add s to vector of strings */
for (auto& w : vs) /* output each word using range-based loop */
std::cout << w << "\n";
}
例子Use/Output
$ echo "my dog has fleas" | ./bin/readcintostrings
my
dog
has
fleas
检查一下,如果您还有其他问题,请告诉我。
我用这段代码弄明白了:
#include <iostream>
#include <string>
int main()
{
std::string testTemp;
std::string brokenUp[20];
int lastOne = 0;
std::cout << "Enter values\n:";
std::getline(std::cin, testTemp);
for(int current = 0; !testTemp.empty() && testTemp.find(' ') != -1; current++)
{
brokenUp[current] = testTemp.substr(0, testTemp.find(' '));
testTemp.erase(0, testTemp.find(' ') + 1);
lastOne = current;
}
lastOne++;
brokenUp[lastOne] = testTemp;
//this next part is just a test
for(int i = 0; i <= lastOne; i++)
{
std::cout << brokenUp[i] << std::endl;
}
}
但您可以使用任何东西作为分解字符串的存储(即列表或动态数组)。