Thread: Reading a file, storing words in a vector using C++ algorithms?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    Question Reading a file, storing words in a vector using C++ algorithms?

    Is it possible to store the contents of a file in a vector of strings, with the delimiter being whitespace...using C++ algorithms and function objects?
    Oh yeah, and the vector's size cannot be predetermined...I'd like to push_back each word until eof.

    I see a problem with this approach:
    -algorithms such as generate(...) and for_each(...), i don't believe, take back_inserter_iterators, so not declaring a vector size would be trouble.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Of course:

    Code:
    int main( void )
    {
        std::string word;
        std::vector<std::string> file;
    
        std::ifstream in( "thing.thing" );
    
        while ( in >> word )
            file.push_back( word );
    
        for ( size_t i=0; i<file.size(); i++ )
            std::cout<< file.at(i) << '\n';
    
        in.close();
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    I was hoping to reduce this problem, traditionally solved in your manner, to a neat and sophisticated algorithm like generate or for_each, preferrably generate because it makes the most sense.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This is based on the examples in "Thinking in C++, vol. 2", generic algorithms.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    
    int main()
    {
        std::ifstream fin(__FILE__);
        std::vector<std::string> vec;
    
        //input from file
        std::copy(std::istream_iterator<std::string>(fin), std::istream_iterator<std::string>(), std::back_inserter(vec));
    
        //output to screen
        std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
    }

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Code:
    copy(std::istream_iterator<std::string>(file), istream_iterator<std::string>(), std::back_inserter(vec));
    Back_inserter works fine for anything that has a .push_back()
    Last edited by grib; 04-26-2007 at 08:43 AM. Reason: dup, gotta hit preview more often

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess another question would be how to do this with lines. I'm not sure, because istream_iterator doesn't let you specify a delimiter, so I guess one could try with a wrapper around a string that represents a line.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    
    class Line
    {
        std::string content;
        friend std::istream& operator >> (std::istream& is, Line& line)
        {
            std::getline(is, line.content);
            return is;
        }
        friend std::ostream& operator << (std::ostream& os, const Line& line)
        {
            return os << line.content;
        }
    };
    
    int main()
    {
        std::ifstream fin(__FILE__);
        std::vector<Line> vec;
        std::copy(std::istream_iterator<Line>(fin), std::istream_iterator<Line>(), std::back_inserter(vec));
        std::copy(vec.begin(), vec.end(), std::ostream_iterator<Line>(std::cout, "\n"));
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Storing Words from a text file
    By matt_570 in forum C++ Programming
    Replies: 18
    Last Post: 12-10-2008, 12:35 PM
  3. Reading words and analyzing words from file
    By desmond5 in forum C Programming
    Replies: 7
    Last Post: 02-26-2008, 03:51 PM
  4. Reading a File and returning single words
    By kapri in forum C++ Programming
    Replies: 5
    Last Post: 05-11-2005, 05:13 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM