Vector Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 9966
    New Member
    • Sep 2007
    • 16

    Vector Problem

    Greetings,

    I'm currently facing a problem on how to compare 2 vector's elements. For example, assuming we have declared 2 integer type vectors:

    vector <int> v1;
    vector <int> v2;

    and both vectors have some integer values stored inside. What is the syntax that I can use to check such that all the elements in v1 and v2 are the same? Can I do the following? Do I need to declare something to overload the == operator? If yes, can tell me how please?

    if(v1 == v2)
    { }

    -----------------------------------------------------------------------------------

    Second Question..
    If the above syntax can be used, then I have the following problem which requires some suggestion. It's cracking my head right now..

    Let's say if there are similar elements found between these 2 vectors. Once the similar element is found, I want to remove that particular element from v1 and the problem that I faced is shown by "????". Since I do not know which element will be found as similar, so what am I suppose to fill into that "????"

    for(int i=0; i < v1.size() ; i++)
    {
    if(v1 == v2)
    {
    v1.erase(v1.beg in() + ????);
    }
    }

    I really want to understand this and any advice would be much appreciated.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by 9966
    Greetings,

    I'm currently facing a problem on how to compare 2 vector's elements. For example, assuming we have declared 2 integer type vectors:

    vector <int> v1;
    vector <int> v2;

    and both vectors have some integer values stored inside. What is the syntax that I can use to check such that all the elements in v1 and v2 are the same? Can I do the following? Do I need to declare something to overload the == operator? If yes, can tell me how please?

    if(v1 == v2)
    { }

    -----------------------------------------------------------------------------------

    Second Question..
    If the above syntax can be used, then I have the following problem which requires some suggestion. It's cracking my head right now..

    Let's say if there are similar elements found between these 2 vectors. Once the similar element is found, I want to remove that particular element from v1 and the problem that I faced is shown by "????". Since I do not know which element will be found as similar, so what am I suppose to fill into that "????"

    for(int i=0; i < v1.size() ; i++)
    {
    if(v1 == v2)
    {
    v1.erase(v1.beg in() + ????);
    }
    }

    I really want to understand this and any advice would be much appreciated.
    You can compare two items in a vector by doing this:
    [code=cpp]
    if (v1[ind] == v2[ind]) // ind is an integer
    {}
    [/code]

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      Originally posted by 9966
      Greetings,

      I'm currently facing a problem on how to compare 2 vector's elements. For example, assuming we have declared 2 integer type vectors:

      vector <int> v1;
      vector <int> v2;

      and both vectors have some integer values stored inside. What is the syntax that I can use to check such that all the elements in v1 and v2 are the same? Can I do the following? Do I need to declare something to overload the == operator? If yes, can tell me how please?

      if(v1 == v2)
      { }

      -----------------------------------------------------------------------------------

      Second Question..
      If the above syntax can be used, then I have the following problem which requires some suggestion. It's cracking my head right now..

      Let's say if there are similar elements found between these 2 vectors. Once the similar element is found, I want to remove that particular element from v1 and the problem that I faced is shown by "????". Since I do not know which element will be found as similar, so what am I suppose to fill into that "????"

      for(int i=0; i < v1.size() ; i++)
      {
      if(v1 == v2)
      {
      v1.erase(v1.beg in() + ????);
      }
      }

      I really want to understand this and any advice would be much appreciated.
      You can use v1==v2 to compare vectors,it's valid,but just as ilikepython said you can use operator[] or vector.at() to compare them if you want to find elements that differ form each other.Other alternative is to use STL's mismatch algorithm defined in algorithm.h.

      regards,

      Savage

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Look into using iterators rather than an int to go through your vectors. Once you find an element in common between v1 and v2, you'll be able to call v1.erase(v1Iter ator), and you should be fine. Make you you look into how that erase function changes the iterator, as you may have to -- it after the erase to make everything work properly.

        Comment

        • hsn
          New Member
          • Sep 2007
          • 237

          #5
          i agree with Ganon11
          iterators are a very good way to deal with vectors

          Comment

          Working...