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 many spaces for tab character(\t)?
- How do I build a graphical user interface in C++? [closed]
- What are the differences between a pointer variable and a reference variable in C++?
- outputting ascii table in C++
- What is the effect of extern “C” in C++?
- Loop through an array in JavaScript
- What exactly is the difference between “pass by reference” in C and in C++?
- When should I write the keyword ‘inline’ for a function/method?
- C++ [Error] no matching function for call to
- When does a process get SIGABRT (signal 6)?
- Logical XOR operator in C++?
- C++ – Decimal to binary converting
- Alternative to itoa() for converting integer to string C++?
- 1e-9 or -1e9, which one is correct?
- Returning an empty string : efficient way in c++
- invalid conversion from ‘const char*’ to ‘char*’
- Evaluate a string with a switch in C++ [duplicate]
- Vector is not a Template?
- How do you format code in Visual Studio Code (VSCode)?
- Displaying a vector of strings in C++
- ‘vector’ in namespace ‘std’ does not have a template type
- Floating Point Exception C++ Why and what is it?
- Issue with std::stol – ‘std::invalid_argument’ what(): stol
- Writing a LinkedList destructor?
- VC++ fatal error LNK1168: cannot open filename.exe for writing
- Creating a copy constructor for a linked list
- Is there a tab equivalent of std::endl within the standard library?
- Using the fstream getline() function inside a class
- Is there ‘byte’ data type in C++?
- How to return a string from a C++ function?
- C++, random number w/ range of 1-6
- What does “missing template argument” mean?
- How do conversion operators work in C++?
- What is the difference between isdigit() and isnumber()?
- Convert char* to string C++
- C++ delete vector, objects, free memory
- Copy a file in a sane, safe and efficient way
- c++ convert string to hex
- LINK : fatal error LNK1561: entry point must be defined ERROR IN VC++
- Multiple OR or AND conditions in IF statement
- What does it mean double free detected in tcache 2 while using mpz?
- What is the difference between iterator and iterable and how to use them?
- fatal error LNK1169: one or more multiply defined symbols found in game programming
- How to initialize a vector of pointers
- c++ Read from .csv file
- Check if C++ Array is Null
- Random not declared in scope
- What does the “Expected ‘(‘ for function-style cast or type construction” error mean?
- LINK : fatal error LNK1561: entry point must be defined ERROR IN VC++
- Comparing the values of char arrays in C++
- Win32 Console Application
- What does the fpermissive flag do?
- Creating an instance of class
- Destructor for a doubly-linked list that points to its value
- cin.ignore(numeric_limits
::max(), ‘\n’) - LLVM Compiler 2.0: Warning with “using namespace std;”
- C++ operator+ and operator+= overloading
- In c++ what does a tilde “~” before a function name signify?
- Too many arguments to function
- not declared in this scope’ when using strlen()
- Converting a Cubemap into Equirectangular Panorama
- glibc detected : double free or corruption
- What is the best way to develop a C++ web application?
- How to install Visual Studio Build Tools 2010 on Visual Studio 2015 Community?
- Throwing out of range exception in C++
- Is null reference possible?
- c++ vector bubble sort
- Write a program that asks the user to enter five test scores. Correspond it to a letter grade
- compare and swap vs test and set
- Weighted random numbers
- compare and swap vs test and set
- Link error “undefined reference to `__gxx_personality_v0′” and g++ [duplicate]
- How to use bitmask?
- How do I check if a Key is pressed on C++