Thread: Store a vector<string> into another vector

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    1

    Store a vector<string> into another vector

    Hi all, first post here so if I do anything against the rules of this community feel free to point that out.


    Basically I'd have to write a very simple program that opens a .txt file, stores each word of each line into a "typedef vector<string> Tlinea" and each line into a "typedef vector<Tlinea> TTexto".


    Using the Ttexto vector I should be able to print on screen the entire original text and to go through the text to find the blank spaces for each line.


    I have written a program which gives the output expected using only the variable Ttlinea, however when I start using the Ttexto variable (part commented) all goes wrong.


    This is my code:


    Code:
    
    #include <iostream>
    #include <vector>
    #include <fstream>
    using namespace std;
    
    
    
    
    
    
    typedef vector <string> TLinea;
    typedef vector<TLinea> TTexto;
    string line;
    
    
    
    
    
    
    int main()
    {
        TLinea my_linea;
        TTexto my_texto;
    
    
        ifstream test ("Ejemplo.txt");
    
    
    
    
        if(!test){
            cout<<"Error opening output file"<<endl;
            return 0;
        }
        while (getline(test,line)){
    
    
        //for (int i = 0 ; i < my_linea.size(); i++){
            my_linea.push_back(line);
            cout << line ;
            int total_blank_spaces = 0;
            for (int t = 0; t < line.size(); t++){
                if (line[t]== ' ') total_blank_spaces++;
            }
    
    
            cout << "   ["Blank spaces: " << total_blank_spaces << "]";
            cout << endl;
            }
        cout << endl;
    
    
    /*Ttexto
        my_texto.push_back(my_linea);
        cout << my_texto[0] << endl;
    */
    
    
    
    
        return 0;
    }

    Thanks in advance for any advice or possible solution.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > cout << my_texto[0] << endl;
    my_texto[0] is a vector of strings.

    Try outputting my_texto[0][0] instead.

    Now would be a good time to get out of the habit of lazily declaring global variables for no good reason, when local variables would be perfectly adequate.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're not dividing the line up into words. You're just counting blanks. I thought linea was supposed to be a vector of words, so instead of pushing the entire line to it, you should be pushing individual words. When you've done that for an entire line then you need to push that to texto.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Vector of string from another vector of strings
    By subdene in forum C++ Programming
    Replies: 13
    Last Post: 08-03-2016, 09:11 AM
  2. Replies: 4
    Last Post: 03-21-2016, 09:38 AM
  3. Replies: 9
    Last Post: 09-22-2013, 07:00 AM
  4. Copying string vector to char vector
    By Ducky in forum C++ Programming
    Replies: 21
    Last Post: 07-10-2013, 11:22 AM
  5. Replies: 1
    Last Post: 04-29-2009, 12:46 PM

Tags for this Thread