Thread: vector search and replace

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    vector search and replace

    Hi,
    i got a vector<int> of values 12,49,51,98

    i wanted to search for value 12 and replace it with another value like 42.

    is there any vector function to search and replace straight away?

    rather than manually looping and checking and then replacing.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's find, which will find an element.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Perhaps the std::replace() function?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> is there any vector function

    You'll find that many of the functions you want to use that work on vector and other standard containers are not member functions. Instead they are non-member functions that are made to be generic so that they can work on many different container classes. A lot of these functions can be found in the <algorithm> header, including std::find and std::replace.

    Note that a vector isn't sorted (unless you do it), so if you are trying to maintain a sorted ordering you'd have to be careful about what you replace with what.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to replace the one particular element with the given value then tabstop's suggestion of std::find() would be most appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    58
    thanks for reply.


    my program was all the while acesing the vector by index only. and the find function uses iterators. i got redesign my program now.

    # was my program design wrong of using vector by index search and not using iterator?
    any way to use find without using iterator?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rahulsk1947
    my program was all the while acesing the vector by index only. and the find function uses iterators. i got redesign my program now.
    You do not have to re-design your program. You can get the corresponding index of the vector from an iterator by subtracting an iterator to the first element of the vector, i.e., by using the begin() member function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replace Array
    By SARAHCPS in forum C Programming
    Replies: 9
    Last Post: 11-15-2005, 11:07 AM
  2. Replace a list with a new entry?
    By niponki in forum C Programming
    Replies: 4
    Last Post: 08-17-2005, 10:41 AM
  3. Replies: 5
    Last Post: 05-25-2004, 04:36 PM
  4. How to replace strings in a file?
    By Doh in forum C Programming
    Replies: 6
    Last Post: 11-26-2002, 12:16 PM
  5. Need a new way to replace a constant
    By RustGod in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2001, 03:05 PM