473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Use Iterator to obtain vector index

I am creating a sorted vector using lower_bound. I set an Iterator to lower_bound to determine the place a 'val' needs to be. I then PushBack(val) to create the needed space for the item. I have tried several methods to then move the the 'val' from the back to it's lower_bound location, but either get a segfault or the items are not in "less than" order. I can easily do this using the 'index' of the items vs. trying to use Iterators. How can I calculate the index of the Iterator for lower_bound? Wait, now that I'm writing this, I have an idea. Can I start at Begin() and have a variable to 'count' up to the lower_bound?
Apr 19 '10 #1
1 1700
Banfa
9,065 Expert Mod 8TB
Why don't you just use the member function vector<>::insert?

Your method is almost bound to fail at some point because a push_back can cause an internal buffer reallocation of the vector and that in turn invalidates all iterators.

You should be using vector but if you are going to use any library class then read the documentation first http://www.cplusplus.com/reference/stl/vector/
Apr 19 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
13
by: Adam Hartshorne | last post by:
Hi All, I was wondering if anybody can tell me what is wrong with the following code, in a .h file I have std::list<std::vector<Site> > positions ; std::list<std::vector<Site> >::iterator...
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
2
by: Piotr | last post by:
Can I create a Random Access Iterator which start at a certain index and end at a certain index of a container? I go thru this page, but I can't find an example....
3
by: utab | last post by:
Hi, there Assume that I have three vectors x,y and z that has the same number of elements. Can I use a common iterator for these three. I did it with iterx, itery and iterz but the thing I...
18
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct...
6
by: happyvalley | last post by:
Hi, I want to remove some elements from a vector, the following code doesn't work, seems it doesn't allow me to remove an element when iterating the vector. (make sense), just wonder, how to do...
5
by: mimi | last post by:
It seems that iterator could be treated as the pointer to object. But I am quite doubt about it. Is using &(*iterator) instead of the pointer to object(after copy the object from the iterator)...
7
by: Renzr | last post by:
I have a problem about the std::set<>iterator. After finding a term in the std::set<>, i want to know the distance from the current term to the begin(). But i have got a error. Please offer me...
6
by: abir | last post by:
Hi, I has a vector<intand i want to have some additional data associated with a few locations in the vector. i.e I want to have unordered_map<vector<int>::iterator, MyClass> so that given a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.