How to assign split strings from one vector to one element of another vector. Ex: v [oldLine] [OldLine]

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

/*Função para fazer split*/
std::vector<std::string> split(std::string s, char c)
{
    std::string buff{""};
    std::vector<std::string> v;

    for(auto n:s)
    {
        if(n != c) buff+=n; 
        else if(n == c && buff != "") { v.push_back(buff); buff = ""; }
    }
    if(buff != "") v.push_back(buff);

    return v;
}
using namespace std;

int main()
{   
    /*Quero passar os elementos(linhas) desse vector...*/
    std::vector<std::string> strOrigin;
    strOrigin.push_back("Uma frase legal aqui...");
    strOrigin.push_back("Uma frase mais legal ainda aqui");
    /*Para esse vector, de forma que eu possa acessar os elementos assim: strCurrent[i][j], onde i eram as linhas do meu vector antigo, e j são os elementos de cada linha*/ 
    std::vector<std::string> strCurrent;
    auto IteratorOrigin = strOrigin.begin();
    strCurrent.push_back(split(IteratorOrigin[0], ' '));

return 0;
}

I want a new vector to receive elements of the strings that I have taken from another vector, so that I can access from the new vector this way: VectorName [i] // In element i of this vector , would be the elements of the string of line i of the previous vector [j] // and that would be the elements. The error that is occurring with this code is as follows:

exit status 1
main.cpp: In function 'int main()':
main.cpp:28:43: error: could not convert 'strOrigin' from 'std::vector<std::__cxx11::basic_string<char> >' to 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}'
  strCurrent.push_back(split(strOrigin, ' '));

Okay, I need a for to do with more than one line of a vector, to give an automated, but in case, I can not even do it with a line ..... kkk

    
asked by anonymous 22.04.2018 / 21:57

1 answer

1

Notice that strCurrent is of class std::vector<std::string> , that is, a vector whose elements belong to class std::string . But you do not want each element of this vector to be a string, but rather a vector of strings, in order to store the result of the function split .

The error message itself says that you are returning std::vector<std::__cxx11::basic_string<char> > (that is, std::vector<std::string> ) of the split function, and trying to use that object as an argument to push_back() , which expects std::__cxx11::string that is, std::string ).

To solve your problem, simply declare strCurrent as

std::vector< std::vector<std::string> > strCurrent;

within your main() function.

Still, you can define a function that performs the loop you want:

std::vector< std::vector<std::string> > split(const std::vector<std::string> & strOrigin, const char separator)
{
  std::vector< std::vector<std::string> > strCurrent(strOrigin.size());
  for(auto&& line : strOrigin)
  {
    strCurrent.push_back( split(line, separator) );
  }
  return strCurrent;
}

Consider also entering the const specifier for your function parameters and passing s by reference:

std::vector<std::string> split(const std::string & s, const char c);
    
28.04.2018 / 17:59