Thread: How to get value at particular index from a vector?

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

    Question How to get value at particular index from a vector?

    Hello evervbody

    How to get value at particular index from a vector?

    Thanks in advance
    Ketu

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I wrote a member method to do this just last week. Iterate through the vector and keep a counter as you go. When you hit the "nTH" element, return the vector element. Not too efficient with real large vectors, but it works.

    Todd

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    Thanks a lot Todd burch.
    Is this means that there is no such method in STL library?
    I mean predefined method in STL...

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I don't know. I'm new to C++ myself, and I'm still reading the books. I created a get_element() method - here's the function I came up with: ("sentences" is a vector<Sentence *> vector.

    (oops - the method I described was my first implementation - this one is better - I forgot I changed it. Glad I looked!! I return NULL if the requested element does not exist.)

    Code:
    Sentence * Sentences::get_element(unsigned int n) { 
    	Sentence * temp = NULL ;
    	vector<Sentence *> * s ; 
    
    	s = &sentences ; 
    	
    	if (n >= s->size()) return temp ; 
    	return (*s)[n] ; 
    }

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    If you're using an STL vector, remember it is random access, so you may access any element using the index operator

    Code:
    std::vector<int> a;
    
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    
    std::cout << a[0] << " " << a[1] << " " << a[2] << std::endl;
    Prints out

    Code:
    1 2 3
    Edit: if you want to search for a particular index where a value is stored, you'd do something like

    Code:
    int index(const vector<int>& a, int key)
    {
    
      for(int i = 0; i < a.size(); ++i)
      {  
          if(a[i] == key)   //key found at index i
             return i;
      }
     
        return -1;   //key not found
    }
    
    std::vector<int> a;
    
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    
    std::cout << index(a, 2) << std::endl;
    prints out

    Code:
    1
    Last edited by indigo0086; 01-02-2008 at 09:35 AM.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You can use std::distance() from the <iterator> header.
    Code:
    std::vector<int> vec;
    std::vector<int>::iterator it;
    ...
    std::vector<int>::difference_type diff = std::distance( vec.begin(), it );

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    the random access operator ([]), doesn't throw, so if you want something that does use .at(). Works the same as [] otherwise, to the best of my knowledge.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Todd Burch View Post
    I wrote a member method to do this just last week. Iterate through the vector and keep a counter as you go. When you hit the "nTH" element, return the vector element. Not too efficient with real large vectors, but it works.

    Todd
    Are you serious?!

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yep! Well, that was my first hack. See subsequent post. Obviously, I figured it out.

  10. #10
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Did I see a joke in here somwhere...this is the C board

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Did I see a joke in here somwhere...this is the C board
    It's in the C++ Programming board, and I do not recall it being in the C programming board otherwise I would have moved it.
    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

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Yep! Well, that was my first hack. See subsequent post. Obviously, I figured it out.

    Todd Burch, I hope that you are not actually doing that in your code. If you are, please say it so that we can help educate you about containers and vectors in particular.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I had it in there for about 2 hours, and now it's long gone.

    I'm Just learning C++ from books, while writing a commissioned application at the same time! !!!

    Trust me - if I can't find an answer from the books when I run across my next stumbling block - I'll certainly let you all educate me! I'm not too proud to ask for help when it's warranted.

    Todd

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Sounds good. These things can be quite confusing if you get unlucky and don't stumble across the text that explains them very well.

    Here's how I might re-do your posted code:
    Code:
    Sentence * Sentences::get_element(unsigned int n) { 
    	if (n >= sentences.size())
    		return 0;
    	return sentences[n]; 
    }
    Actually, I think I misread your code the first time. I guess you already figured out that you shouldn't be iterating through the vector. What you posted still had a bunch of unnecessary stuff in it, but it wasn't as bad as I made it sound.

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Thanks for the tweak - I will implement that "return 0" instead of allocating an empty pointer. I'm new to C as well - only been coding in it a few months now.

    While some code in that method appears superfluous for the example at hand (and it is), in my actual code, based on the state of processing, get_element() might return a pointer from one of two different vectors. That's why I set up variable s with the address of one vector. I removed some of my code to post, but didn't rewrite it to be as simple as it could be.

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM