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?
- How to dynamically allocate arrays in C++
- How to convert string to char array in C++?
- How can I convert a std::string to int?
- Why is “using namespace std;” considered bad practice?
- What is a lambda expression in C++11?
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- How to convert string to char array in C++?
- ld: symbol(s) not found for architecture x86_64 error
- C++ string to double conversion
- Undefined Symbols for architecture x86_64: Compiling problems
- “No viable overloaded ‘=’ ” why?
- Check if a string contains a string in C++
- How does #include
work in C++? - Efficient way to return a std::vector in c++
- “No viable overloaded ‘=’ ” why?
- Replace part of a string with another string
- Error with multiple definitions of function
- ‘do…while’ vs. ‘while’
- Does the ‘mutable’ keyword have any purpose other than allowing the variable to be modified by a const function?
- Visual Studio /**/ comment shortcut?
- SDL2.DLL missing
- Argument list for class template is missing
- Fastest way to check if a file exist using standard C++/C++11,14,17/C?
- How do you add a timed delay to a C++ program?
- Cleanest way to copy a constant size array in c++11
- Difference between using Makefile and CMake to compile the code
- Exception Error c0000005 in VC++
- push_back vs emplace_back
- “g++” is not recognized as an internal or external command, MinGW
- Compiling a C++ program with gcc
- C++ BlackJack Stuck trying to program Ace
- Convert a String In C++ To Upper Case
- Is std::stoi actually safe to use?
- Visual Studio 2015 doesn’t have cl.exe
- Right way to split an std::string into a vector
- How to resize array in C++?
- Is the sizeof(some pointer) always equal to four?
- How do you initialise a dynamic array in C++?
- What are the distinctions between the various symbols (*,&, etc) combined with parameters?
- Accessors and Mutators C++
- Error C1083: Cannot open include file: ‘stdafx.h’
- How do you handle a “cannot instantiate abstract class” error in C++?
- How to cast the size_t to double or int C++
- How to get Current Directory?
- C++ error: object of abstract class type is not allowed: pure virtual function has no overrider
- Correct way to work with vector of arrays
- What’s the difference between nexti and stepi in gdb?
- How to get current time in milliseconds?
- Resizing dynamic array in c++
- C++ – Nested include – Avoiding ‘include nested too deeply error’
- Why is inherited member not allowed?
- Error: macro names must be identifiers using #ifdef 0
- Error: free(): invalid next size (fast):
- Py_Initialize fails – unable to load the file system codec
- C++ pass an array by reference
- error LNK2019: unresolved external symbol “” referenced in function
- Undefined reference to a static member
- How to sort with a lambda?
- stoi function gives error: std::invalid_argument at memory location 0x0035E8D8. c++
- Stack around the variable ‘ ‘ was corrupted
- The difference between cin.ignore and cin.sync
- casting int to char using C++ style casting
- Simple way to create matrix of random numbers
- Unrecognizable template declaration/definition
- MSVCP120d.dll missing
- Modulo operator with negative values [duplicate]
- Finding the type of an object in C++
- C++ Simple Dice roll – how to return multiple different random numbers [duplicate]
- Whats the difference between UInt8 and uint8_t
- Struct with template variables in C++
- error: no member function declared in class
- Why does my program give “NULL used in arithmetic”
- no matching function to call for “getline”
- How to iterate (keys, values) in JavaScript?