How to Iterate Over a Constant Vector

How do I iterate over a Constant Vector?

You have two (three in C++11) options: const_iterators and indexes (+ "range-for" in C++11)

void func(const std::vector<type>& vec) {
std::vector<type>::const_iterator iter;
for (iter = vec.begin(); iter != vec.end(); ++iter)
// do something with *iter

/* or
for (size_t index = 0; index != vec.size(); ++index)
// do something with vec[index]

// as of C++11
for (const auto& item: vec)
// do something with item
*/
}

You should prefer using != instead of < with iterators - the latter does not work with all iterators, the former will. With the former you can even make the code more generic (so that you could even change the container type without touching the loop)

template<typename Container>
void func(const Container& container) {
typename Container::const_iterator iter;
for (iter = container.begin(); iter != container.end(); ++iter)
// work with *iter
}

Is it possible to iterate over a `const vectorint`?

1) you need a vector<int>::const_iterator to iterate through a "const" container

2) Your vector will still get copied; this may or may not be desireable. I like to return references...

Iterate through a const vector of pointers with auto

Is it correct to do

It depends on you.

For the 1st case, the type of item will be Component* const&, which is a reference bound to the element of the vector.

For the 2nd case, the type of item will be Component*, which (i.e. the pointer itself) is copied from the the element of the vector.

The parameter type of method is Component*, means passing the pointer by value/copy, then there's no actual difference between the 2 cases. And for pointer (as built-in type), just auto item : components is fine here.

How to iterate over a const vectorglm::vec3

This error is because you declared the std::vector as constant:

std::vector<glm::vec3> const& vertices

Then when you declared a loop of iterators you used mutable iterators (i.e. non-constant iterators which allow you to write to the values pointed to by the iterator). This is in direct violation to your original declaration of the vector. You can fix this by using a constant iterator to do the loop as such:

for(std::vector<glm::vec3>::const_iterator it = vertices.begin(); it != vertices.end(); ++it) {
...
}

Or if you do require mutating access (i.e. you want to write to the data) by declaring the original declaration of the vector without the "const" keyword as such:

std::vector<glm::vec3>& vertices

How to iterate over const object with iterator?

Use std::vector<>::const_iterator.

How to iterate through a container within a const container?

Really, do yourself a favor and use range based for loops:

for (auto&& [key, vec] : m) {
for (auto&& vec_element : vec) {
// All vec elements
}
}

If you don't want to use C++17 (you should, C++17 is great!) then do that:

for (auto&& m_element : m) {
for (auto&& vec_element : m_element.second) {
// All vec elements
}
}

If you really want to use use iterators (you really should use range based for loops) then continue reading.

You are not using iterator correctly. You are using a iterator that points on an element in the map, then get it's key to get back the element. This is not how to use an iterator.

Instead, you should use the object the iterator is pointing to directly.

for (auto start_iter = m.begin(); start_iter != m.end(); ++start_iter) {   
for (auto vector_iter = start_iter->second.begin(); vector_iter != start_iter->second.end(); ++vector_iter) {
// shuff with vector_iter
}
}

Now why does operator[] failed?

That's because it's a std::map. A with a map, you should be able to create something on the fly:

// creates element at key `123`
m[123] = {"many", "strings", "in", "vector"};

For the element to be created by the container at a new key, it must save that new element in itself, so it must mutate itself to provide operator[], so it's not const.

You could have also use std::map::at(), but it isn't worth it in your case. Using either operator[] or map.at(key) will make the map search for the key, which isn't trivial in complexity.

c++: const_iterator and the for-each loop

You probably want your const_iterator to point to a const Vector. You also need to adjust your constructor, like follows:

class const_iterator : public iterator {
public:
const Vector<T>* const ptr;

...
explicit const_iterator(const Vector<T>* v, uint64_t offset){
...
}

That way, your const_iterator constructor should be callable within your range for iteration.

See Difference between const declarations in C++ if you need to understand the details of how the const keyword works in pointer declarations.

How to iterate over a generic vector

See the error message carefully. It says you're trying to convert a function to iterator. You should call them by adding ().

Change

for (typename vector<T>::iterator iter = v.begin; iter != v.end; iter++) {

to

for (typename vector<T>::iterator iter = v.begin(); iter != v.end(); iter++) {
~~ ~~

For C++ books, The Definitive C++ Book Guide and List



Related Topics



Leave a reply



Submit