Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;在动态数组中解析getline_C++_Foreach_Dynamic Arrays - Fatal编程技术网

C++ c++;在动态数组中解析getline

C++ c++;在动态数组中解析getline,c++,foreach,dynamic-arrays,C++,Foreach,Dynamic Arrays,我已经问过了,如何将流中的单个单词解析为变量,这非常有效,但我不知道用户会给出多少单词作为输入。我想我可以把它解析成一个动态数组,但我不知道从哪里开始。我怎么写“一行中的每个字” 这是我如何将单词解析为vars的: string line; getline( cin, line ); istringstream parse( line ); string first, second, third; parse >> first >> second >> thi

我已经问过了,如何将流中的单个单词解析为变量,这非常有效,但我不知道用户会给出多少单词作为输入。我想我可以把它解析成一个动态数组,但我不知道从哪里开始。我怎么写“一行中的每个字”

这是我如何将单词解析为vars的:

string line;
getline( cin, line );
istringstream parse( line );
string first, second, third;
parse >> first >> second >> third;
谢谢


编辑:谢谢大家,我想我知道了。。。而且它有效

您可以使用
std::vector
std::list
——它们自动处理大小调整

istringstream parse( line ); 
vector<string> v;
string data; 
while (parse >> data) {
  v.push_back(data);
}
istringstream解析(行);
向量v;
字符串数据;
while(解析>>数据){
v、 推回(数据);
}

您可以编写如下内容:

string line;
getline( cin, line );
istringstream parse( line );
string word;
while (parse >> word)
    // do something with word

一种可能是与以下设备一起使用:

#包括
#包括
#包括
#包括
#包括
int main()
{
std::istringstream in(std::string(“文件中的一行”);
向量词;
std::copy(std::istream_迭代器(in),
std::istream_迭代器(),
标准:背向插入器(单词);
返回0;
}

向量将根据需要增长,以存储用户提供的任何字数。

由于您将问题标记为
foreach
,以下是一种使用标准
算法和C++11 lambdas实现的方法:

#include <string>
#include <sstream>
#include <iostream>
#include <algorithm> // for for_each
#include <vector>    // vector, obviously
#include <iterator>  // istream_iterator
using namespace std;

int main()
{
    string line;
    vector<string> vec;
    getline(cin, line);

    istringstream parse(line);

    for_each(
        istream_iterator<string>(parse),
        istream_iterator<string>(),

        // third argument to for_each is a lambda function
        [](const string& str) {
             // do whatever you want with/to the string
             vec.push_back(str);  // push it to the vector
        }
    );
 }
#包括
#包括
#包括
#包括//for_
#显然包括向量
#include//istream\u迭代器
使用名称空间std;
int main()
{
弦线;
向量向量机;
getline(cin,line);
istringstream解析(行);
每人(
istream_迭代器(解析),
istream_迭代器(),
//_的第三个参数是lambda函数
[](常量字符串和str){
//你想怎么做就怎么做
vec.push_back(str);//将其推到向量
}
);
}

一个
vector
正是您想要的-一个动态调整大小的数组,您应该总是喜欢它而不是C样式的数组。编译时不需要知道它的大小。

你们可以不断地将它弹出到一个临时变量中,并将其推到一个向量上。好的,我真的很喜欢Alexander Bakulins的答案,因为我马上就明白了,但我想你们推荐vector并将其推回去是有原因的。你能解释一下吗?我没有和vector合作过before@mohrphium,向量是一个动态数组,可以为您进行内存管理。@mohrphium-如果您可以立即处理元素(因此不需要存储),Alexander的答案很好。您正在询问有关存储提取的值的问题。STL集合建议使用原始阵列,因为它们通过自动且良好地执行内存管理(例如,调整大小)来为您处理繁重的存储。如果需要,您甚至可以进行边界检查(例如,使用
at()
函数);或者只使用迭代器。既然你正在用C++编写程序,你应该学习语言的特性——特别是STL——它会让你在长时间里理解AITLAS答案变得更容易,但是我不太能理解迭代器的东西。它是做什么的?@mohrphium,如果你点击答案中的链接,它会给出完整的描述。@mohrphium可能从开始,它会解释什么是迭代器。
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm> // for for_each
#include <vector>    // vector, obviously
#include <iterator>  // istream_iterator
using namespace std;

int main()
{
    string line;
    vector<string> vec;
    getline(cin, line);

    istringstream parse(line);

    for_each(
        istream_iterator<string>(parse),
        istream_iterator<string>(),

        // third argument to for_each is a lambda function
        [](const string& str) {
             // do whatever you want with/to the string
             vec.push_back(str);  // push it to the vector
        }
    );
 }