stl questions: how can I compare 2 stl list?

Discussion in 'C++' started by silverburgh.meryl@gmail.com, Apr 16, 2006.

  1. Guest

    is there a better way to compare 2 stl list? I write a function like
    this below, but I wonder if there is a better way to achieve that (e.g.
    less code)?


    bool isSame(const list<int>& srcList, const list<int>& destList ) {
    if (srcList.size() != destList.size()) {
    return false;
    }

    int size = srcList.size();
    for (int i = 0; i < size; i++) {
    if (srcList != destList) {
    return false;
    }
    }

    return true;
    }

    Thank you.
    , Apr 16, 2006
    #1
    1. Advertising

  2. wrote:
    > is there a better way to compare 2 stl list? I write a function like
    > this below, but I wonder if there is a better way to achieve that
    > (e.g. less code)?
    >
    >
    > bool isSame(const list<int>& srcList, const list<int>& destList ) {
    > if (srcList.size() != destList.size()) {
    > return false;
    > }
    >
    > int size = srcList.size();
    > for (int i = 0; i < size; i++) {
    > if (srcList != destList) {
    > return false;
    > }
    > }
    >
    > return true;
    > }


    I suppose you did not compile this, did you? Try it .. it will fail
    to compile, because std::list does not have an 'operator []'.

    Try this instead:

    bool isSame (std::list <int> const& l1, std::list <int> const& l2)
    {
    if (l1.size () == l2.size ())
    return std::equal (l1.begin (), l1.end (), l2.begin ());
    else
    return false;
    }

    hth
    --
    jb

    (reply address in rot13, unscramble first)
    Jakob Bieling, Apr 16, 2006
    #2
    1. Advertising

  3. lallous Guest

    Hello

    AFAIK, lists implement '==' operator.

    So just test as:

    bool isSame = list1 == list2;

    Also check the std::equal()

    --
    Elias
    <> wrote in message
    news:...
    > is there a better way to compare 2 stl list? I write a function like
    > this below, but I wonder if there is a better way to achieve that (e.g.
    > less code)?
    >
    >
    > bool isSame(const list<int>& srcList, const list<int>& destList ) {
    > if (srcList.size() != destList.size()) {
    > return false;
    > }
    >
    > int size = srcList.size();
    > for (int i = 0; i < size; i++) {
    > if (srcList != destList) {
    > return false;
    > }
    > }
    >
    > return true;
    > }
    >
    > Thank you.
    >
    lallous, Apr 16, 2006
    #3
  4. peter koch Guest

    skrev:

    > is there a better way to compare 2 stl list? I write a function like
    > this below, but I wonder if there is a better way to achieve that (e.g.
    > less code)?
    >
    >
    > bool isSame(const list<int>& srcList, const list<int>& destList ) {
    > if (srcList.size() != destList.size()) {
    > return false;
    > }
    >
    > int size = srcList.size();
    > for (int i = 0; i < size; i++) {
    > if (srcList != destList) {
    > return false;
    > }
    > }
    >
    > return true;
    > }
    >
    > Thank you.


    How about list1 == list2?

    /Peter
    peter koch, Apr 16, 2006
    #4
  5. Guest

    wrote:
    > is there a better way to compare 2 stl list? I write a function like
    > this below, but I wonder if there is a better way to achieve that (e.g.
    > less code)?
    >
    >
    > bool isSame(const list<int>& srcList, const list<int>& destList ) {
    > if (srcList.size() != destList.size()) {
    > return false;
    > }
    >
    > int size = srcList.size();
    > for (int i = 0; i < size; i++) {
    > if (srcList != destList) {
    > return false;
    > }
    > }
    >
    > return true;
    > }
    >
    > Thank you.



    Hello,

    How about operator ==() ?
    See
    "http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=list.html#operator=="
    , Apr 16, 2006
    #5
  6. wrote:
    > is there a better way to compare 2 stl list? I write a function like
    > this below, but I wonder if there is a better way to achieve that (e.g.
    > less code)?
    >
    >
    > bool isSame(const list<int>& srcList, const list<int>& destList ) {
    > if (srcList.size() != destList.size()) {
    > return false;
    > }
    >
    > int size = srcList.size();
    > for (int i = 0; i < size; i++) {
    > if (srcList != destList) {
    > return false;
    > }
    > }
    >
    > return true;
    > }
    >
    > Thank you.


    What's wrong with

    srcList == destList

    ?
    Markus Schoder, Apr 16, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gianni Mariani
    Replies:
    1
    Views:
    861
    Pete Becker
    Aug 16, 2003
  2. Kevin Goodsell
    Replies:
    0
    Views:
    435
    Kevin Goodsell
    Aug 16, 2003
  3. Thomas Matthews
    Replies:
    1
    Views:
    889
    John Villalovos
    Aug 18, 2003
  4. John Villalovos
    Replies:
    0
    Views:
    419
    John Villalovos
    Aug 18, 2003
  5. Martoon
    Replies:
    9
    Views:
    806
    Kai-Uwe Bux
    Apr 20, 2006
Loading...

Share This Page