How to assign split lines(Strings) of a vector to an element of another vector. Ex: v[lineAntiga][elementLinhaAntiga]

Asked

Viewed 72 times

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(lines) I took from another vector, so that I can access, from the new vector, like this: nameDoVector[i]/In the i element 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 happening 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 go to do with more than one vector line, to do an automated one, but in this case, I can’t even do it with one line.... kkk

1 answer

1

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

The error message itself says you are returning std::vector<std::__cxx11::basic_string<char> > (namely, a std::vector<std::string>) of function split, and trying to use this object as an argument to push_back(), waiting for a std::__cxx11::string (that is to say, std::string).

To solve your problem, simply declare strCurrent as

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

within its function main().

Still, you can define a function that executes 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 specifier const the parameters of its function and pass s by reference:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.