c++ - 在 C++ 中分离流

标签 c++ sorting input vector

    int n = 1; 

    //Lines 14 to (vertices+13) read
    while (i<(vertices+14))
    {
        //First three numbers on each line put into "v1"
        if (n < 4)
        {
            copy(istream_iterator<float>(input), 
            istream_iterator<float>(), 
            back_inserter(v1));

            n++;
        }

        //Last three numbers on each line put into "v2"
        else
        {
            copy(istream_iterator<float>(input), 
            istream_iterator<float>(), 
            back_inserter(v2));
            n++;

            if (n > 6)
            {
                i++;    
                n=1;    
            }
        }

这应该将一行中的前三个数字排序到一个 vector 中,将最后三个数字排序到另一个 vector 中。然而,由于我怀疑流的工作方式的性质,所有数字都进入 vector v1,没有一个进入 v2。输入是一个 ifstream。

最佳答案

问题是由于如何std::copy有效。

第二个参数表示复制到哪里停止,还有你的参数

istream_iterator<float>()

表示“流的结尾”。对 copy第一次 调用是读取整个流。

使用std::copy_n而不是复制一定数量。

来自评论:

Is there also a way of using the second parameter of copy to get three numbers?

这正是它的用途。对于一些 n 数,如果您想复制 n 值,请使用 std::copy_n

关于c++ - 在 C++ 中分离流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15818603/

相关文章:

c++ - 使用 OpenMP 的较慢代码如何并行化?

c# - 通过托管 C++ 包装器从非托管 C++ dll 运行 C# 函数

c++ - 我应该从静态成员方法返回什么类型的指针

python - Python 中的字母数字排序和负数

arrays - 按特定顺序排列数组中的元素

html - 为 Mobile Safari (webkit) 重新创建 HTML5 范围输入?

xml - xslt中的输入参数

c++ - 在 C++ 中实现事件并定期驱动 "script language"?

Javascript二次排序

html - 样式输入轮廓边框 - CSS3