How to find Index positions with a vector STL?

This is a discussion on How to find Index positions with a vector STL? within the C++ Programming forums, part of the General Programming Boards category; Hello, vector <int>my_array my_array is 32 in size. The contents of my_array is: 11110000011111110000011111.111 I want to know the 4 ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    4

    How to find Index positions with a vector STL?

    Hello,

    vector <int>my_array

    my_array is 32 in size.

    The contents of my_array is:
    11110000011111110000011111.111


    I want to know the 4 index positions of '0'.


    How do I do it?


    Thanks,
    codeguy

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    I want to know the 4 index positions of '0'.
    What do you mean by the "4 index positions of '0'"?
    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

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    I meant the four in color:

    11110000011111110000011111.111


    -codeguy

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Iterate through the vector. When the number changes from 1 to 0 or from 0 to 1, make a note.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess you'll have to loop over the entire vector, keep a count of (consequtive?) 0-s and then decide whether a particular 0 is the one that you are interested in.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    There's also the std::find() function in the <algorithm> header, but then you'll have to convert the iterator it returns to an index (try the std::distance() function in <iterator>).

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,313
    Quote Originally Posted by codeguy View Post
    I meant the four in color:

    11110000011111110000011111….111
    Er okay, cause there's no way we could have guessed that from the previous post!

    Do you mean that each integer in the vector holds a single 1 or 0, or do you mean to treat this as some kind of bitset where each int holds 32 1's or 0's?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  2. deleting a node by index
    By mouse163 in forum C++ Programming
    Replies: 4
    Last Post: 02-24-2005, 07:13 AM
  3. Getting the index of a list box to show in a msgbox
    By Mystical in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2004, 03:17 PM
  4. file index update error
    By daluu in forum C Programming
    Replies: 1
    Last Post: 04-28-2003, 02:47 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

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