C++;通过copy()输出 我正在玩C++和STL,我尝试把DEQE复制到一个列表中,并通过CopyTo和OrthyType迭代器打印列表。出于某种原因,除非我通过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"); } }

C++;通过copy()输出 我正在玩C++和STL,我尝试把DEQE复制到一个列表中,并通过CopyTo和OrthyType迭代器打印列表。出于某种原因,除非我通过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"); } },c++,algorithm,stl,iterator,ostream,C++,Algorithm,Stl,Iterator,Ostream,编辑: 为了更容易理解为什么对提出相同问题的人来说这不起作用,下面是copy_if()的语义。很明显,它不会扩展容器: template <class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator copy_if (InputIterator first, InputIterator last, OutputIterator r

编辑:

为了更容易理解为什么对提出相同问题的人来说这不起作用,下面是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;
}
模板
输出迭代器复制\u如果(输入迭代器第一,输入迭代器最后,
输出计算器结果,一元谓词pred)
{
while(第一个!=最后一个){
if(pred(*first)){
*结果=*第一;
++结果;
}
++第一,;
}
返回结果;
}

复制
复制\u如果
没有将新元素添加到
列表中
,则他们假定存在要复制到的现有元素。您的列表最初是空的,因此您正在写入列表的迭代器。这不会增加列表大小(这就是前两次尝试不打印任何内容的原因),但是如果您访问(实际上不存在)第一个列表成员,您可能会得到写入其中的结果

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


你可以继续使用
copy
和friends,如果你使用的是一个(你通常会使用的),类似于你已经在使用的
ostream\u迭代器。

因为你的列表是空的,为什么l.front()可以工作?不相关:如果(布尔值)返回true,构造
if;否则返回false是新手常见的错误。它只是枚举所有可能的布尔返回值。把它想象成
开关(int\u值){case 0:return 0;case 1:return 1;case 2:return 2;//etc
。你也不会这样写。正确的编写方法是
return boolean\u value;
@MSalters,这是一个意见问题,特别是当
boolean\u value
是一个非常重要的表达式时。@VictorBrunell:
l.front()
不起作用,这是空列表中未定义的行为。不幸的是,未定义行为的一个可能结果是“似乎起作用”。另一个结果是“计算机着火”.啊,谢谢。那么,通常的做法是计算要复制的容器中的元素数量,并在要复制的容器中保留足够的空间,还是有更好的习惯用法?太好了。谢谢。
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;
}