首页 > 解决方案 > C++ 通过 copy() 输出

问题描述

我正在使用 C++ 和 STL,我试图将一个双端队列复制到一个列表中并通过 copy() 和 ostream_iterator 打印该列表。出于某种原因,我复制到的列表的内容不会打印,除非我通过 front()、back() 或 at() 访问元素。为什么前两次打印尝试失败:

#include <iostream>
#include <fstream>
#include  <deque>
#include <algorithm>
#include <iterator>
#include <list>
using namespace std;

void alterParticle(string&);

int main(){

   string tmp_str;
   deque<string> d;
   list<string> l;

   ifstream in("foo.txt");
   if(!in.is_open()){
      cout << "Error opening file" << endl;
      return 1;
   }
   while(in){
      getline(in,tmp_str);
      d.push_back(tmp_str);
   }

   for_each(d.begin(),d.end(),alterParticle);

   copy(d.begin(),d.end(),ostream_iterator<string>(cout,"\n"));

   ostream_iterator<string> out(cout,"\n");
   copy_if(d.begin(),d.end(),out,
         [](const string& s){
            if(s.find("fooparticle")!= string::npos)
               return true;
            return false;
         });

   copy_if(d.begin(),d.end(),l.begin(),
      [](const string& s){
         if(s.find("fooparticle")!= string::npos)
            return true;
         return false;
      });

   cout << "First try: " << endl;
   for(string s : l)
      cout << s << endl;

   cout << "Second try: " << endl;
   copy(l.begin(),l.end(),out);

   cout << "Last try: " << l.front() << endl;

   return 0;
}

void alterParticle(string& s){
   int fpos = s.find("quark");
   string rep_str{"quark"};
   if(fpos != string::npos){
      s.replace(s.find(rep_str),rep_str.length(),"fooparticle");
   }
}

输出:

fooparticle 10 11.4
neutrino 7 20.5
electron 5 6.7
proton 8 9.5

fooparticle 10 11.4
First try:
Second try:
Last try: fooparticle 10 11.4

编辑:

只是为了更容易理解为什么这对于提出相同问题的任何人都不起作用,这里是 copy_if() 的语义。很明显,它不会扩展容器:

template <class InputIterator, class OutputIterator, class UnaryPredicate>
  OutputIterator copy_if (InputIterator first, InputIterator last,
                          OutputIterator result, UnaryPredicate pred)
{
  while (first!=last) {
    if (pred(*first)) {
      *result = *first;
      ++result;
    }
    ++first;
  }
  return result;
}

标签: c++algorithmstliteratorostream

解决方案


copy并且copy_if不要向 中添加新元素list,它们假定存在要复制到的现有元素。您的列表最初是空的,因此您正在写入begin() == end()列表的迭代器。这不会增加列表大小(这就是前两次尝试不打印任何内容的原因),但是如果您访问(实际上不存在的)第一个列表成员,您可能会得到写在那里的结果。

不用说,分配给end()迭代器是未定义的行为。

copy如果您使用insert_iterator(您通常使用),您可以继续使用和朋友back_inserter,类似于ostream_iterator您已经使用的。


推荐阅读