简体   繁体   中英

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);

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);
      
    }
  }
  
  
  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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