I've been receiving a constant stream run time errors using vectors.
So, can anyone tell me if the second method is safer?
1. for(vector<type>::iterator it = test_vector.begin(); it != test_vector.end(); it++)
2. for(vector<type>::iterator it = test_vector.begin(); it < test_vector.end(); it++)
My feeling is that the second one is better. If so, why don't I see it too much in tutorials and stuff?
8 1201
I've been receiving a constant stream run time errors using vectors.
So, can anyone tell me if the second method is safer?
1. for(vector<type>::iterator it = test_vector.begin(); it != test_vector.end(); it++)
2. for(vector<type>::iterator it = test_vector.begin(); it < test_vector.end(); it++)
My feeling is that the second one is better. If so, why don't I see it too much in tutorials and stuff?
Note that only random access iterators (as supplied by the vector class)
implement the operator< therefore it's much safer to use the operator==
for your test because that one works for *any* iterator type.
Are you sure you haven't used a const vector? You have to use a const iterator
for those containers.
kind regards,
Jos
Note that only random access iterators (as supplied by the vector class)
implement the operator< therefore it's much safer to use the operator==
for your test because that one works for *any* iterator type.
Are you sure you haven't used a const vector? You have to use a const iterator
for those containers.
kind regards,
Jos
Ok. Here's the problem. I use an iterator, check the value, and the use
the problem here is, after this, sometimes
does not return false even if the iterator is beyond end. I can't pinpoint where my problem is exactly. That is why I thought using < operator would be better in this case. Any thoughts?
If you erase an element from your vector you must not increment the iterator.
I haven't seen your code so this is just a guess.
kind regards,
Jos
Shouldn't the STL container take care of that, though? Does the vector automatically force the iterator to point to the previous element? If not, isn't there a .remove() function in the iterator itself? If so, you may want to use that.
Banfa 9,065
Expert Mod 8TB
If you erase an iterator in a vector then the iterator and all iterators to later entries in the vector become invalid. Following an erase operation you should not use the iterator erased.
However the erase method returns a valid iterator to the next entry in the vector so you should do something like -
IF (I AM GOING TO ERASE THIS ITERATOR)
-
{
-
NEXT ITERATOR = ERASE(THIS ITERATOR)
-
}
-
ELSE
-
{
-
NEXT ITERATOR = THIS ITERATOR+1
-
}
-
Note this pseudo code is showing the logic, it is not meant to imply you need 2 iterator variables.
Also note that when you erase an entry in a vector (via an iterator or otherwise) the vector case to copy all entries in the vector down 1 slot (because memory in a vector is held in 1 contiguous block) so looping down a vector erasing each entry in turn is a particularly inefficient way to empty a vector.
If you erase an iterator in a vector then the iterator and all iterators to later entries in the vector become invalid. Following an erase operation you should not use the iterator erased.
That is not entirely true: a vector never shrinks itself automagically; erasing
an element of a vector just leaves one more unused slot at the end. If your
iterator runs 'upwards' and you don't increment your iterator after a erase and
you dynamically check the v.end() value, all is safe. e.g. the following removes
the odd elements from a vector<int>: -
for (vector<int>::iterator i= v.begin(); i != v.end(); )
-
if (*i&1)
-
v.erase(i);
-
else
-
i++;
In this scenario no new iterator needs to be used. I agree that other iterators
that point beyond this point of deletion and don't have any notion of the
deletion should take great care.
kind regards,
Jos
Banfa 9,065
Expert Mod 8TB
That is not entirely true: a vector never shrinks itself automagically;
I know that :D I didn't think I said it did, however if you thought I did then the clarification is useful.
I know that :D I didn't think I said it did, however if you thought I did then the clarification is useful.
I was just following up on your phrase: "Following an erase operation you should not use the iterator erased."
;-)
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: {AGUT2}=IWIK= |
last post by:
Hello all,
It's my fisrt post here and I am feeling a little stupid here, so go easy..
:) (Oh, and I've spent _hours_ searching...)
I am desperately trying to read in an ASCII...
|
by: luigi |
last post by:
Hi,
I am trying to speed up the perfomance of stl vector by
allocating/deallocating blocks of memory manually. one version of the
code crashes when I try to free the memory. The other version...
|
by: Forecast |
last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully.
: // proj2.cc: returns a dynamic vector and prints out at main~~
: //
: #include <iostream>
: #include <vector>
:
: using...
|
by: Adam Hartshorne |
last post by:
Hi All,
I have the following problem, and I would be extremely grateful if
somebody would be kind enough to suggest an efficient solution to it.
I create an instance of a Class A, and...
|
by: Bob |
last post by:
Here's what I have:
void miniVector<T>::insertOrder(miniVector<T>& v,const T& item)
{
int i, j;
T target;
vSize += 1;
T newVector;
newVector=new T;
|
by: Ross A. Finlayson |
last post by:
I'm trying to write some C code, but I want to use C++'s std::vector.
Indeed, if the code is compiled as C++, I want the container to
actually be std::vector, in this case of a collection of value...
|
by: Martin Jørgensen |
last post by:
Hi,
I get this using g++:
main.cpp:9: error: new types may not be defined in a return type
main.cpp:9: note: (perhaps a semicolon is missing after the definition
of 'vector')
main.cpp:9:...
|
by: Sanjay Kumar |
last post by:
Folks,
I am getting back into C++ after a long time and I have
this simple question: How do pyou ass a STL container
like say a vector or a map (to and from a function) ?
function:
...
|
by: zl2k |
last post by:
hi, there
I am using a big, sparse binary array (size of 256^3). The size may be
changed in run time. I first thought about using the bitset but found
its size is unchangeable. If I use the...
|
by: toton |
last post by:
Hi,
I want to have a vector like class with some additional functionality
(cosmetic one). So can I inherit a vector class to add the addition
function like,
CorresVector : public...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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,...
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |