inserting txt info into a map C++

Can someone please help me.

here is my code so far, i have added notes:
<>
istringstream iss;
iss.str(text_);
string temp;

while (!iss.eof())
{


return (word);

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <sstream>
#include <fstream>

// you may want to adjust this as appropriate for your use-case
std::string remove_punct( std::string str ) // replace punctuation with space
{
    for( char& c : str ) if( !std::isalnum(c) ) c = ' ' ;
    return str ;
}

std::map< std::string, std::set<int> > make_word_map( std::istream& stm )
{
    std::map< std::string, std::set<int> > word_map ;

    int line_number = 0 ;
    std::string line ;
    while( std::getline( stm, line ) ) // for each line read in from the stream
    {
        ++line_number ; // increment the line number

        // split the line into 'words'
        std::istringstream str_stm( remove_punct(line) );
        std::string word ;

        while( str_stm >> word ) // for each 'word' in the line
            word_map[word].insert(line_number) ; // insert word, line_number into the map
            // not that the set will automatically discard duplicate line numbers
    }

    return word_map ;
}

int main()
{
    std::ifstream file( __FILE__ ) ;
    const auto map = make_word_map(file) ;

    // for testing: print out the contents of the map
    for( const auto& pair : map ) // for each key/mapped_value pair in the map
    {
        std::cout << pair.first << " : " ; // print the key (word)
        for( int line_num : pair.second ) // for each line number in the set
            std::cout << line_num << ' ' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/96a63e514a1e03e5
Registered users can post here. Sign in or register to post.