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.
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!!!!
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?
#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( constauto& 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 ;
staticconst 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' ;
}