K
Assuming that the lines in the internal vector components are sorted, standard algorithms may be applied to them. std::set_intersection and std::set_union♪A demonstration programme is presented below.#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
#include <iterator>
int main()
{
std::vector<std::pair<std::string, std::string>> masiv_Y1 =
{
{ "A", "ADF" }, { "B", "BCF" }, { "C", "BC" },
{ "D", "ADF" }, { "E", "E" }, { "F", "ABDF" }
};
for ( const std::pair<std::string, std::string> &p : masiv_Y1 )
{
std::cout << "{ " << p.first << ", " << p.second << " } ";
}
std::cout << std::endl;
std::vector<std::pair<std::string, std::string>> masiv_Y2;
for ( auto current = masiv_Y1.begin(); current != masiv_Y1.end(); ++current )
{
for ( auto next = std::next( current ); next != masiv_Y1.end(); ++next )
{
std::string second;
std::set_intersection( current->second.begin(), current->second.end(),
next->second.begin(), next->second.end(),
std::back_inserter( second ) );
if ( !second.empty() )
{
std::string first;
std::set_union( current->first.begin(), current->first.end(),
next->first.begin(), next->first.end(),
std::back_inserter( first ) );
masiv_Y2.push_back( { first, second } );
}
}
}
for ( const std::pair<std::string, std::string> &p : masiv_Y2 )
{
std::cout << "{ " << p.first << ", " << p.second << " } ";
}
std::cout << std::endl;
return 0;
}
Her withdrawal to the console{ A, ADF } { B, BCF } { C, BC } { D, ADF } { E, E } { F, ABDF }
{ AB, F } { AD, ADF } { AF, ADF } { BC, BC } { BD, F } { BF, BF } { CF, B } { DF, ADF }
When using your containers, i.e. the vectors invested, the programme may look as follows:#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
int main()
{
typedef std::vector<std::string> String1D;
typedef std::vector<String1D> String2D;
String2D masiv_Y1 =
{
{ "A", "ADF" }, { "B", "BCF" }, { "C", "BC" },
{ "D", "ADF" }, { "E", "E" }, { "F", "ABDF" }
};
for ( const String1D &p : masiv_Y1 )
{
std::cout << "{ " << p[0] << ", " << p[1] << " } ";
}
std::cout << std::endl;
String2D masiv_Y2;
for ( auto current = masiv_Y1.begin(); current != masiv_Y1.end(); ++current )
{
for ( auto next = std::next( current ); next != masiv_Y1.end(); ++next )
{
std::string second;
std::set_intersection( ( *current )[1].begin(), ( *current )[1].end(),
( *next )[1].begin(), ( *next )[1].end(),
std::back_inserter( second ) );
if ( !second.empty() )
{
std::string first;
std::set_union( ( *current )[0].begin(), ( *current )[0].end(),
( *next )[0].begin(), ( *next )[0].end(),
std::back_inserter( first ) );
masiv_Y2.push_back( { first, second } );
}
}
}
for ( const String1D &p : masiv_Y2 )
{
std::cout << "{ " << p[0] << ", " << p[1] << " } ";
}
std::cout << std::endl;
return 0;
}