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?
- Return array in a function
- Regular cast vs. static_cast vs. dynamic_cast
- What is a smart pointer and when should I use one?
- check if a std::vector contains a certain object?
- Convert char to int in C and C++
- How to convert a char array to a string?
- How to fix a “invalid operands to binary expression” error?
- Is it possible to decompile a C++ executable file
- Difference between `constexpr` and `const`
- C++ Singleton design pattern
- Difference between long double and double in C and C++ [duplicate]
- segmentation fault 11 in C++ on Mac
- How to append text to a text file in C++?
- Why there is no pop_front method in C++ std::vector?
- How do I generate a random number between two variables that I have stored?
- How to print pthread_t
- python – if not in list
- make: g++: Command not found
- What is an ‘undeclared identifier’ error and how do I fix it?
- What does the question mark character (‘?’) mean in C++?
- Use of “this” keyword in C++
- Why am I getting “undefined reference to main”
- How to avoid the error: terminate called after throwing an instance of ‘std::logic_error’ what(): basic_string::_S_construct null not valid
- Error while overloading operator (must be a nonstatic member function)
- C++ Fatal Error LNK1120: 1 unresolved externals
- Vector of structs initialization
- C++ deprecated conversion from string constant to ‘char*’
- How to read groups of integers from a file, line by line in C++
- Converting a vector
to string - std::string formatting like sprintf
- Sorting Characters Of A C++ String
- Sentinel while loop for C++
- How to convert vector to array
- “non-standard syntax; use ‘&’ to create a pointer to member” error in Visual Studio 2015
- libpng warning: iCCP: known incorrect sRGB profile
- C++: Expression must have a constant value when declaring array inside function
- For loop and ‘numpy.float64’ object is not iterable error
- What does ‘const static’ mean in C and C++?
- How to determine if a string is a number with C++?
- C++ wait for user input
- non-standard syntax; use ‘&’ to create a pointer to member
- How to shuffle a std::vector?
- Difference between ‘struct’ and ‘typedef struct’ in C++?
- Debug vs Release in CMake
- C++ Matrix Class
- access violation reading location c++
- C++ template constructor
- PlaySound in C++ Console application?
- What is the difference of pairs() vs. ipairs() in Lua?
- Creation of Dynamic Array of Dynamic Objects in C++
- Compiling simple Hello World program on OS X via command line
- C++ Compiler Error C2280 “attempting to reference a deleted function” in Visual Studio 2013 and 2015
- Why use pointers?
- variable-sized object may not be initialized c++
- Why I cannot cout a string?
- How do I return a char array from a function?
- Can a for loop increment/decrement by more than one?
- c++ reading csv file
- Why do you use typedef when declaring an enum in C++?
- Converting String to Cstring in C++
- creating dynamic array of string c++
- How well is Unicode supported in C++11?
- The CXX compiler identification is unknown
- Is there a standard sign function (signum, sgn) in C/C++?
- “No rule to make target ‘install'”… But Makefile exists
- How to determine the version of the C++ standard used by the compiler?
- Make / gcc cryptic error 2: how to have more information?
- REGSVR32: the module “xxxxx.dll” failed to load … dependent assembly could not be found
- Error: expression cannot be used as a function?
- What is a .h.gch file?
- MinGW .exe requires a few gcc dll’s regardless of the code?
- “for loop” with two variables?
- Template constructor in a class template – how to explicitly specify template argument for the 2nd parameter?
- C++ float array initialization