|
No.23074,DD.さんのコードで十分と思います。
「上質」かどうか分かりませんが、"find_if"を使った例:
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
struct InString {
const std::string& s_;
InString( const std::string& s ) : s_(s){}
bool operator()( const std::string& elem ) const
{
return elem.find( s_ ) != std::string::npos;
}
};
int main()
{
std::vector< std::string > vs1;
vs1.push_back( "abcd" );
vs1.push_back( "efgh" );
vs1.push_back( "ijkl" );
vs1.push_back( "mnop" );
vs1.push_back( "qrst" );
vs1.push_back( "uvwx" );
vs1.push_back( "yz" );
std::vector< std::string > vs2;
std::string target = "mn";
std::vector< std::string >::iterator it = std::find_if( vs1.begin(), vs1.end(), InString( target ) );
while( it != vs1.end() ){
vs2.push_back( *it );
it = std::find_if( ++it, vs1.end(), InString( target ) );
}
if( !vs2.empty() ){
std::copy( vs2.begin(), vs2.end(), std::ostream_iterator< std::string >( std::cout, " " ) );
}
else {
std::cout << "not found";
}
std::cout << std::endl;
}
|