vector search and replace

This is a discussion on vector search and replace within the C++ Programming forums, part of the General Programming Boards category; Hi, i got a vector<int> of values 12,49,51,98 i wanted to search for value 12 and replace it with another ...

  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,185
    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,328
    >> 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
    21,794
    If you want to replace the one particular element with the given value then tabstop's suggestion of std::find() would be most appropriate.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10: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, 11:16 AM
  5. Need a new way to replace a constant
    By RustGod in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2001, 02:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21