简体   繁体   English

如何使用迭代器将向量推回到向量的向量上

[英]How to push back a vector onto a vector of vectors using an iterator

I have this code that creates an error -我有这段代码会产生错误-

#include <vector>
#include <iostream>
#include <string>

void read_string(std::string &str,
         std::vector<std::string> &dir,
         std::vector<std::vector<std::string> > &table,
         std::vector<std::vector<std::string> > &result)
{

  std::vector<std::vector<std::string> >::iterator  it0;
  std::vector<std::string>::iterator it1;
  
  /*intent, iterate over each  element (vector of strings) of the
    vector of elements */
  for(it0 = table.begin(); it0 != table.end(); it0++){
    
    /*code to select specific vector of strings -added back to show intent*/
    if((*it0)[0]==str){

        /*selected vector of strings(element) are added to new table
        called "result" */
        for(it1 = (*it0).begin(); it1 != (*it0).end(); it1++){
        result.push_back(*it1);
      }
    }
  }
  
  
  
}

test.cc:18:28: error: no matching function for call to 'std::vectorstd::vector<std::__cxx11::basic_string<char > >::push_back(std::__cxx11::basic_string&)' result.push_back(*it1); test.cc:18:28: error: no matching function for call to 'std::vectorstd::vector<std::__cxx11::basic_string<char >>::push_back(std::__cxx11::basic_string&)' 结果.push_back(*it1);

The intent of this code is to copy table onto result.此代码的目的是将表复制到结果上。 What is the proper solution for this?什么是正确的解决方案? In other words how would you copy a vector of vectors onto another vector of vectors?换句话说,您如何将一个向量向量复制到另一个向量向量上?

Oops, yes too many layers (I should have understood the first comment) - This is the desired code -哎呀,是的太多层(我应该理解第一条评论) - 这是所需的代码 -

void read_string(std::string &str,
         std::vector<std::string> &dir,
         std::vector<std::vector<std::string> > &table,
         std::vector<std::vector<std::string> > &result)
{

  std::vector<std::vector<std::string> >::iterator  it0;
  std::vector<std::string>::iterator it1;
  

  //std::copy_if(table, table.size(), [](std::string p_str) {return
    
  for(it0 = table.begin(); it0 != table.end(); it0++){

    if((*it0)[0]==str){
    
      result.push_back(*it0);
      
    }
  }
  
  
  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM