I want to extract a maximum of N + 1 strings from a std::stringstream
.
Currently, I have the following code (that needs to be fixed):
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
#include <iterator>
#include <ranges>
#include <algorithm>
int main( )
{
const std::string_view sv { " @a hgs -- " };
const size_t expectedTokenCount { 4 };
std::stringstream ss;
ss << sv;
std::vector< std::string > foundTokens;
foundTokens.reserve( expectedTokenCount + 1 );
std::ranges::for_each( std::ranges::take_view { ss, expectedTokenCount + 1 }, [ &foundTokens ]( const std::string& token )
{
std::back_inserter( foundTokens );
} );
if ( foundTokens.size( ) == expectedTokenCount )
{
// do something
}
for ( const auto& elem : foundTokens )
{
std::cout << std::quoted( elem ) << '\n';
}
}
How should I fix it? Also, how should I use back_inserter
to push_back
the extracted strings into foundTokens
?