Я пытаюсь создать шаблон, который сравнивает векторы векторов строк, игнорируя последние 2 строки векторов строк.В идеале шаблон функции должен возвращать 2 противоположных набора разностей (diff и del) и 1 набор пересечений (красный).Однако это вывод:
diff:
del:
red: 1001 01 100101
1101 11 110111
111110 111110
Программа завершилась с кодом выхода: 0
Я исхожу из: https://ideone.com/yJHN0L
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <iterator>
#include <assert.h>
template<class InputIt1, class InputIt2,
class OutputIt1, class OutputIt2, class OutputIt3,
class Compare>
OutputIt3 decompose_connections(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt1 result1, OutputIt2 result2,
OutputIt3 result3, Compare comp)
{
while (first1 != last1 && first2 != last2) {
if (comp(*first1, *first2)) {
*result1++ = *first1++;
} else if (comp(*first2, *first1)) {
*result2++ = *first2++;
} else {
*result3++ = *first1++;
++first2;
}
}
std::copy(first1, last1, result1);
std::copy(first2, last2, result2);
return result3;
}
int main() {
// Simply as an example
std::vector<std::string> A1, A2, A3, B1, B2, B3;
std::vector<std::vector<std::string>> vecA, vecB, diff, del, red;
std::string strA1_1 = "1101";
std::string strA1_2 = "11";
std::string strA1_3 = "110111";
std::string strA2_1 = "1111";
std::string strA2_2 = "10";
std::string strA2_3 = "111110";
std::string strA3_1 = "1001";
std::string strA3_2 = "01";
std::string strA3_3 = "100101";
std::string strB1_1 = "1001";
std::string strB1_2 = "11";
std::string strB1_3 = "100111";
std::string strB2_1 = "0111";
std::string strB2_2 = "10";
std::string strB2_3 = "011110";
std::string strB3_1 = "1001";
std::string strB3_2 = "01";
std::string strB3_3 = "100101";
A1.push_back(strA1_1); A1.push_back(strA1_2); A1.push_back(strA1_3);
A2.push_back(strA2_1); A2.push_back(strA2_2); A2.push_back(strA2_3);
A3.push_back(strA3_1); A3.push_back(strA3_2); A3.push_back(strA3_3);
B1.push_back(strB1_1); B1.push_back(strB1_2); B1.push_back(strB1_3);
B2.push_back(strB2_1); B2.push_back(strB2_2); B2.push_back(strB2_3);
B3.push_back(strB3_1); B3.push_back(strB3_2); B3.push_back(strB3_3);
vecA.push_back(A1); vecA.push_back(A2); vecA.push_back(A3);
vecB.push_back(B1); vecB.push_back(B2); vecB.push_back(B3);
// end example
auto compare = [](std::vector<std::vector<std::string>>::const_reference lhs,
std::vector<std::vector<std::string>>::const_reference rhs)
{
assert(lhs.size() && rhs.size());
return lexicographical_compare(lhs.begin(), prev(lhs.end(), 2),
rhs.begin(), prev(rhs.end(), 2)
);
};
std::sort(vecA.begin(), vecA.end());
std::sort(vecB.begin(), vecB.end());
decompose_connections(vecA.begin(), vecA.end(),
vecB.begin(), vecB.end(),
std::back_inserter(diff),
std::back_inserter(del),
std::back_inserter(red),
compare);
std::cout << "diff: " << endl;
for (int i = 0; i < diff.size(); i++) {
for (int j = 0; j < diff[i].size(); j++) {
std::cout << diff[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
std::cout << "del: " << endl;
for (int i = 0; i < del.size(); i++) {
for (int j = 0; j < del[i].size(); j++) {
std::cout << del[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
std::cout << "red: " << endl;
for (int i = 0; i < red.size(); i++) {
for (int j = 0; j < red[i].size(); j++) {
std::cout << red[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}