Hello evervbody
How to get value at particular index from a vector?
Thanks in advance
Ketu
Hello evervbody
How to get value at particular index from a vector?
Thanks in advance
Ketu
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
Thanks a lot Todd burch.
Is this means that there is no such method in STL library?
I mean predefined method in STL...
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] ; }
If you're using an STL vector, remember it is random access, so you may access any element using the index operator
Prints outCode: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;
Edit: if you want to search for a particular index where a value is stored, you'd do something likeCode:1 2 3
prints outCode: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;
Code:1
Last edited by indigo0086; 01-02-2008 at 09:35 AM.
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 );
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.
Yep! Well, that was my first hack. See subsequent post. Obviously, I figured it out.
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.Did I see a joke in here somwhere...this is the C board
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>> 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.
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
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: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.Code:Sentence * Sentences::get_element(unsigned int n) { if (n >= sentences.size()) return 0; return sentences[n]; }
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