【问题标题】:c++ list splicing helpc++列表拼接帮助
【发布时间】:2011-08-30 10:20:37
【问题描述】:

我正在尝试拼接此列表,但在调用拼接时收到错误消息,提示没有匹配函数。据我所知,我所有的#include 都是正确的。

错误来自于调用 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, " "));
}

【问题讨论】:

  • 如果您不是 100% 确定它们,您应该考虑在代码中添加您的#include(s)。
  • 当我在 temp 上调用任何东西时发生错误,我知道的任何函数都包含在列表中

标签: c++ list iterator splice


【解决方案1】:

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

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

或者你当然可以通过引用传递它,这取决于你。

【讨论】:

    【解决方案2】:

    拼接函数的原型声明为:

    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);
    

    【讨论】:

      猜你喜欢
      • 2020-05-02
      • 2018-02-09
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 2020-09-03
      相关资源
      最近更新 更多