c ++列表拼接帮助

时间:2011-05-22 00:41:47

标签: c++ list iterator splice

我正在尝试拼接此列表,但是当我调用splice时,我收到一条错误,指出没有匹配的功能。据我所知,我所有的#includes都是正确的。

错误来自调用temp的每一行。

void DeckOps::encrypt(int msize, list<int> *L){

    int jokeA = 27;
    int jokeB = 28;
    string keystream;

    list<int>::iterator a = std::find(L->begin(), L->end(), jokeA);
    list<int>::iterator new_position = a;
    for(int i=0; i < 1 && new_position != L->begin(); i++)
        new_position--;

    L->insert(new_position, 1, *a);

    L->erase(b);

    list<int>::iterator aa = L->begin();
    int sec;
    for(; aa != L->end(); aa++){
        if(*aa == jokeA || *aa == jokeB)
            break;  //aa is at 1st inlist either 27 or 28                                                                                                    
    }
    if(*aa == jokeA){
        sec = jokeA;
    } else {
        sec = jokeB;
    }
    list<int>::iterator bb = std::find(L->begin(), L->end(), sec);   

    // everything works up to this point it seems
    list<int> temp;
    temp.splice(temp.end(), L, aa, bb);
    temp.splice(temp.end(), L, bb);
    temp.splice(temp.end(), L, begin(), aa);
    L->splice(L->end(), L, aa);
    L->splice(L->end(), temp);

    //testing                                                                                                                                          
    std::copy(L->begin(), L->end(), std::ostream_iterator<int>(std::cout, " "));
}

2 个答案:

答案 0 :(得分:4)

L是一个指针,你需要取消引用它。

temp.splice(temp.end(), *L, aa, bb);

或者您可以通过引用将其传递给您,直到您。

答案 1 :(得分:1)

splice函数的原型声明为:

void splice ( iterator position, list<T,Allocator>& x );
void splice ( iterator position, list<T,Allocator>& x, iterator i );
void splice ( iterator position, list<T,Allocator>& x, iterator first, iterator last );

您传递给函数的第二个参数L被声明为指针:

list<int> *L

这导致无匹配函数错误,因为没有将第二个参数作为指针的拼接函数。您必须取消引用指针L以匹配splice的函数原型。

temp.splice(temp.end(), *L, aa, bb);
temp.splice(temp.end(), *L, bb);
temp.splice(temp.end(), *L, begin(), aa);