Is there any reason I don’t see this in C++? Is it bad practice?
No. It is not a bad practice, but the following approach renders your code certain flexibility.
Usually, pre-C++11 the code for iterating over container elements uses iterators, something like:
std::vector<int>::iterator it = vector.begin();
This is because it makes the code more flexible.
All standard library containers support and provide iterators. If at a later point of development you need to switch to another container, then this code does not need to be changed.
Note: Writing code which works with every possible standard library container is not as easy as it might seem to be.
Related Posts:
- Iterate through a C++ Vector using a ‘for’ loop
- Iterate through a C++ Vector using a ‘for’ loop
- How to iterate through a list of objects in C++?
- How to navigate through a vector using iterators? (C++)
- C++ for each, pulling from vector elements
- Python `if x is not None` or `if not x is None`?
- Pointer to incomplete class type is not allowed
- Why use string::iterator rather than index?
- Single quotes vs. double quotes in Python
- Using “super” in C++
- How to iterate over a vector?
- Fill array with random numbers within a specified range (C++)
- How can I print out C++ map values?
- Enhanced FOR loops in C++
- How to iterate through two lists in parallel?
- What is the difference between const_iterator and non-const iterator in the C++ STL?
- How can I iterate through a string and also know the index (current position)?
- Using true and false in C
- “…redeclared as different kind of symbol”?
- What is the preferred way to include error messages in C++?
- #pragma once vs include guards?
- Nested For – Loops to create multiplication table C++
- why can’t I dereference an iterator?
- Iterator Loop vs index loop
- Why is this vector iterator not incrementable?
- List iterator not dereferencable?
- Is “delete this” allowed in C++?
- What is a segmentation fault?
- How many spaces for tab character(\t)?
- How to create a dynamic array of integers
- How to create a dynamic array of integers
- Linker Error C++ “undefined reference ” [duplicate]
- How do I build a graphical user interface in C++? [closed]
- C++ convert from 1 char to string?
- How do I build a graphical user interface in C++? [closed]
- What does the “yield” keyword do?
- convert a char* to std::string
- system(“pause”); – Why is it wrong?
- Pause Console in C++ program
- system(“pause”); – Why is it wrong?
- Pause Console in C++ program
- How to implement 2D vector array?
- Why the switch statement cannot be applied on strings?
- What is the difference between g++ and gcc?
- How to use setprecision in C++
- How to dynamically allocate arrays in C++
- What does (~0L) mean?
- How to dynamically allocate arrays in C++
- What is the best way to use a HashMap in C++?
- What is the best way to use a HashMap in C++?
- What are the differences between a pointer variable and a reference variable in C++?
- Why do we need virtual functions in C++?
- Why in C++ do we use DWORD rather than unsigned int? [duplicate]
- What is an undefined reference/unresolved external symbol error and how do I fix it?
- Sleep for milliseconds
- How to convert string to char array in C++?
- How can I convert a std::string to int?
- What does the “yield” keyword do?
- Easiest way to convert int to string in C++
- What is the difference between float and double?
- Why is “using namespace std;” considered bad practice?
- What is the easiest way to initialize a std::vector with hardcoded elements?
- Python for-in loop preceded by a variable
- What is the C++ function to raise a number to a power?
- Java 8 Iterable.forEach() vs foreach loop
- outputting ascii table in C++
- What is a lambda expression in C++11?
- Vector of Vectors to create matrix
- How to use the PI constant in C++
- What is the difference between ++i and i++?
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- How to throw a C++ exception
- std::string to char*
- How to generate a random number in C++?
- pinpointing “conditional jump or move depends on uninitialized value(s)” valgrind message
- What is an undefined reference/unresolved external symbol error and how do I fix it?
- How do I find the length of an array?
- Sleep for milliseconds
- How to convert string to char array in C++?
- C++ — expected primary-expression before ‘ ‘
- Using getline() with file input in C++
- What is the effect of extern “C” in C++?
- C++ Cout & Cin & System “Ambiguous” [closed]
- lvalue required as left operand of assignment error when using C++
- Loop through an array in JavaScript
- ld: symbol(s) not found for architecture x86_64 error
- C++ string to double conversion
- Why are #ifndef and #define used in C++ header files?
- Loop through an array in JavaScript
- g++ ld: symbol(s) not found for architecture x86_64
- Best way to return multiple values from a function? [closed]
- What exactly is the difference between “pass by reference” in C and in C++?
- How to use the PI constant in C++
- What is the difference between ++i and i++?
- How to throw a C++ exception
- Conversion from string to char – c++
- How do I fix a “Expected Primary-expression before ‘)’ token” error?
- How to find out if an item is present in a std::vector?
- What is meant by Resource Acquisition is Initialization (RAII)?
- When to use extern “C” in simple words? [duplicate]