Thread: vectors with same iterators

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    vectors with same iterators

    Dear All
    I have a vector that has several elements with same greatest value. If I try with max_element he will return only iterator that point on the first element of vector with the greatest value. But I want to erase all elements in the second vector that are on the same position as alements with the greatest value of the first vector.
    I have tried with loop, but I could only get integer or indexes with which I cannot erase elements of the second vector.
    Thank you

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    A solution could be to get the iterator to the max element as you did, then iterate over all elements and compare the iterator value until you find the correct one. Not the most elegant solution, but couldn't figure out something better at the moment.
    Code:
    max = std::max_element(v1.begin(), v1.end());
    
    i1 = v1.begin();
    i2 = v2.begin();
    while(i1 != v1.end())
    {
      if(i1 == max)
      {
        v1.erase(i1);
        v2.erase(i2);
        break;
      }
    
      i1++;
      i2++;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The solution was already given, wasn't it?

    Code:
    int index = std::distance(vec.begin(), some_iterator_in_vec);
    And to generate an iterator for another vector out of it:

    Code:
    vector<int>::iterator some_iterator_in_other_vec =
      std::advance(other_vec.begin(), index);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Never heard of or used std::advance or std::distance before. You learn something new every day.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors and Iterators
    By Trev614 in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2008, 10:30 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. vectors and iterators?
    By jcafaro10 in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2007, 11:39 AM
  5. Vectors, iterators and loops
    By Heavens in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2007, 06:05 AM