C++ 使用Lambda函数调用模板-错误

C++ 使用Lambda函数调用模板-错误,c++,string,templates,vector,lambda,C++,String,Templates,Vector,Lambda,我正在尝试创建一个模板来比较字符串向量,忽略字符串向量的最后两个字符串。理想情况下,函数模板应返回两组相反的差异(diff和del)和一组相交(红色)。但是,这是输出: 差异: 德尔: 红色: 100101100101 110111111 111110111110 程序以退出代码结束:0 我是基于: #包括 #包括 #包括 #包括 #包括 #包括 模板 OutputIt3分解_连接(InputIt1 first1,InputIt1 last1, InputIt2 first2,InputIt2

我正在尝试创建一个模板来比较字符串向量,忽略字符串向量的最后两个字符串。理想情况下,函数模板应返回两组相反的差异(diff和del)和一组相交(红色)。但是,这是输出:

差异:

德尔:

红色: 100101100101

110111111

111110111110

程序以退出代码结束:0

我是基于:

#包括
#包括
#包括
#包括
#包括
#包括
模板
OutputIt3分解_连接(InputIt1 first1,InputIt1 last1,
InputIt2 first2,InputIt2 last2,
输出1结果1,输出2结果2,
输出3结果3,比较组件)
{
while(first1!=last1&&first2!=last2){
如果(公司(*first1,*first2)){
*结果1++=*first1++;
}否则如果(公司(*first2,*first1)){
*结果2++=*first2++;
}否则{
*结果3++=*first1++;
++前2名;
}
}
std::copy(first1、last1、result1);
std::复制(first2,last2,result2);
返回结果3;
}
int main(){
//举个简单的例子
标准:载体A1,A2,A3,B1,B2,B3;
标准::向量向量向量、向量b、差异、del、红色;
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=“11111 0”;
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.向后推(strA1_1);A1.向后推(strA1_2);A1.向后推(strA1_3);
A2.向后推(strA2_1);A2.向后推(strA2_2);A2.向后推(strA2_3);
A3.向后推(strA3_1);A3.向后推(strA3_2);A3.向后推(strA3_3);
B1.向后推(strB1)和B1.向后推(strB1和2);B1.向后推(strB1和3);
B2.后推式(strB2_1);B2.后推式(strB2_2);B2.后推式(strB2_3);
B3.向后推(strB3\U 1);B3.向后推(strB3\U 2);B3.向后推(strB3\U 3);
vecA.向后推(A1);vecA.向后推(A2);vecA.向后推(A3);
向量推回(B1);向量推回(B2);向量推回(B3);
//结束示例
自动比较=[](标准::向量::常量参考lhs,
标准::矢量::常数(参考rhs)
{
断言(lhs.size()&&rhs.size());
返回字典的比较(lhs.begin(),prev(lhs.end(),2),
rhs.begin(),prev(rhs.end(),2)
);
};
排序(vecA.begin(),vecA.end());
排序(vecB.begin(),vecB.end());
分解_连接(vecA.begin(),vecA.end(),
vecB.begin(),vecB.end(),
标准:背面插入器(差异),
标准:背面插入器(del),
标准:背面插入器(红色),
比较);

std::恕我道歉。我已经用示例向量更新了它。无法用进行复制。编译器和版本是什么?也无法用g++进行复制。我明白了,这是Xcode的问题。我是基于以下内容:为什么输出只有以下内容:diff:del:red:10010110011011111111111011111011110110
#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;
}