You’re close.
std::list<Student>::iterator it; for (it = data.begin(); it != data.end(); ++it){ std::cout << it->name; }
Note that you can define it
inside the for
loop:
for (std::list<Student>::iterator it = data.begin(); it != data.end(); ++it){ std::cout << it->name; }
And if you are using C++11 then you can use a range-based for
loop instead:
for (auto const& i : data) { std::cout << i.name; }
Here auto
automatically deduces the correct type. You could have written Student const& i
instead.
Related Posts:
- Iterate through a C++ Vector using a ‘for’ loop
- Iterate through a C++ Vector using a ‘for’ loop
- Iterate through a C++ Vector using a ‘for’ loop
- push_back vs emplace_back
- push_back vs emplace_back
- What is the difference between const_iterator and non-const iterator in the C++ STL?
- Use the auto keyword in C++ STL
- Iterator Loop vs index loop
- List iterator not dereferencable?
- What is the easiest way to initialize a std::vector with hardcoded elements?
- What is a lambda expression in C++11?
- ld: symbol(s) not found for architecture x86_64 error
- Meaning of = delete after function declaration
- C++ Double Address Operator? (&&)
- C++ std::priority_queue uses the lambda expression
- Usage and Syntax of std::function
- What exactly is nullptr?
- How to navigate through a vector using iterators? (C++)
- C++ for each, pulling from vector elements
- How do I erase an element from std::vector<> by index?
- Split a string using C++11
- Is it more efficient to copy a vector by reserving and copying, or by creating and swapping? [duplicate]
- How to create timer events using C++ 11?
- How do I reverse a C++ vector?
- How does #include
work in C++? - What does T&& (double ampersand) mean in C++11?
- Segmentation fault error 11 C++
- Why doesn’t C++ have a garbage collector?
- A proper way to create a matrix in c++
- Compiling C++11 with g++
- terminate called after throwing an instance of ‘std::invalid_argument’ what(): stoi
- What exactly is std::atomic?
- Issue with std::stol – ‘std::invalid_argument’ what(): stol
- terminate called after throwing an instance of ‘std::out_of_range’
- Pointer to incomplete class type is not allowed
- Cleanest way to copy a constant size array in c++11
- Why use string::iterator rather than index?
- undefined reference to ‘std::cout’
- expression preceding parentheses of apparent call must have (pointer-to-) function type
- C++ error: Undefined symbols for architecture x86_64
- C++ terminate called without an active exception
- Thread pooling in C++11
- How to find if a given key exists in a C++ std::map
- Parsing a comma-delimited std::string
- How does std::forward work? [duplicate]
- Correct way of looping through C++ arrays
- How to ensure that a std::map is ordered?
- Difference in make_shared and normal shared_ptr in C++
- How to iterate over a vector?
- What is the difference between set and hashset in C++ STL?
- What is std::move(), and when should it be used?
- How can I print out C++ map values?
- C++: std does not have member “string”
- How to iterate through two lists in parallel?
- Remove spaces from std::string in C++
- Remove spaces from std::string in C++
- How to check that an element is in a std::set?
- What’s the most efficient way to erase duplicates and sort a vector?
- Best way to extract a subvector from a vector?
- How to sum up elements of a C++ vector?
- How can I iterate through a string and also know the index (current position)?
- Implementation of Vector in C++
- Calculate average in java
- Appending a vector to a vector
- Call to implicitly deleted copy constructor in LLVM
- Determine if map contains a value for a key?
- Initializing a static std::map
in C++ - cc1plus: error: unrecognized command line option “-std=c++11” with g++
- “…redeclared as different kind of symbol”?
- What is the difference of pairs() vs. ipairs() in Lua?
- error C2679: binary ‘<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
- Nested For – Loops to create multiplication table C++
- why can’t I dereference an iterator?
- How to get current time in milliseconds?
- no match for ‘operator<<’ in ‘std::operator
- Warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11?
- Call to non-static member function without an object argument compiler error
- Does C++11 have C#-style properties?
- std::string length() and size() member functions
- initialize a vector to zeros C++/C++11
- When is it safe to call this-> in constructor and destructor
- How to memset char array with null terminating character?
- sorting in std::map where key is a std::string
- How well is Unicode supported in C++11?
- How to do std::string indexof in C++ that returns index of matching string?
- Why should I use a pointer rather than the object itself?
- too many initializers for ‘int [0]’ c++
- overloaded function with no contextual type information
- Creating folders in C++
- Why is this vector iterator not incrementable?
- convert string to size_t
- Why can’t I make a vector of references?
- C++ equivalent of StringBuffer/StringBuilder?
- Compiling C++11 with g++
- ERROR C2039: ‘vector’: is not a member of ‘std’
- Displaying contents of a vector container in C++
- Does static constexpr variable inside a function make sense?
- c++ vector bubble sort
- How to use bitmask?
- Differences between unique_ptr and shared_ptr