How to set a map?!!!

I have a vector like: vector<string> Phrase;
and a map like: map<int,Frase> AllPhrases;

And the text like:
What if to taste and see, to notice things. To stand each is up against emptiness
for a moment or an eternity. Images collected in consciousness like a tree alone on the horizon.

And the result of the map has to be:

1 What if to taste and see, to notice things.
2 To stand each is up against emptiness for a moment or an eternity.
3 Images collected in consciousness like a tree alone on the horizon.

Please how can I do that?

Thanks a lot
Is your test already separated out into the three different phrases, in that vector<string> Phrase?

If so, then:

1) Iterate over the elements in Phrase.
2) For each element, add an entry to AllPhrases, that has the element index as a key, and the element itself as the value.
3) PROFIT!!!!
int n = 1;
string s;
while(cin >>s){
int k = s.size()-1;
if(s[k] != '.'){
Phrase.push_back(s);
}
else{
Phrase.push_back(s);
AllPhrases[n] = Phrase();
Phrase.clear();
++n;
}
}

I have this but it doesn't work, it doesn't compile :'(
Is there any reason why you chose to withhold all that helpful information your compiler is giving you about why it doesn't compile, and which lines the errors are on?

Why are you clearing Phrase when the last element of s is a

The line:

AllPhrases[n] = Phrase();
has what looks like a function call to a function called Phrase. I suspect that's not what you really wanted to do, right?
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
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <iomanip>

// join the lines into a single string, separate one line from the other with a space
std::string join( const std::vector<std::string>& lines )
{
    std::string text ;

    for( const auto& str : lines ) text += str + ' ' ; // add a space after each line

    if( !text.empty() ) text.pop_back() ; // remove the last space

    return text ;
}

// split the text into ". " separated phrases
std::map< int, std::string > make_phrases( const std::string& text )
{
    std::map< int, std::string > all_phrases ;

    static const std::string seperator = ". " ;

    int key = 0 ;
    std::size_t start_pos = 0 ;
    std::size_t end_pos ;
    while( start_pos < text.size() && ( end_pos = text.find( seperator, start_pos ) ) != std::string::npos )
    {
        // end_pos+1 since the period is part of the phrase
        all_phrases.emplace( ++key, text.substr( start_pos, end_pos+1-start_pos ) ) ;
        start_pos = end_pos+2 ;
    }

    // add the last fragment, if any
    if( start_pos < text.size() ) all_phrases.emplace( ++key, text.substr(start_pos) ) ;

    return all_phrases ;
}

int main()
{
    std::stringbuf stm_buf( "What if to taste and see, to notice\n"
                            "things. To stand each is up against emptiness\n"
                            "for a moment or an eternity. Images collected in\n"
                            "consciousness like a tree alone on the horizon.\n" ) ;
    std::cout << "lines being read from stdin\n---------------------\n" << stm_buf.str() << "------------\n\n" ;
    std::cin.rdbuf( std::addressof(stm_buf) ) ;

    // read line by line into a vectot
    std::vector<std::string> lines ;

    // add each complete line read from stdin to the vector
    std::string ln ;
    while( std::getline( std::cin, ln ) ) lines.push_back(ln) ;

    for( auto& pair : make_phrases( join(lines) ) )
        std::cout << pair.first << ". " << std::quoted(pair.second) << '\n' ;
}

http://coliru.stacked-crooked.com/a/d1ebd49bd718c2c7
Topic archived. No new replies allowed.