In C++11, why use a for loop when you can use a foreach loop?
#include <iostream> //for std::cout void foo() { for (Person const& person : personList) { std::cout << person << ' '; } }
In C++98/03, why use a for
loop when you can use an algorithm instead?
#include <iterator> //for std::ostream_iterator #include <algorithm> //for std::copy #include <iostream> //for std::cout void foo() { std::copy( personList.begin(), personList.end(), std::ostream_iterator(std::cout, " ") ); }
Note that this works with any pair of iterators, not only those from std::set<t>
. std::copy
will use your user-defined operator<<
to print out every item inside the set
using this single statement.
Related Posts:
- outputting ascii table in C++
- What is the effect of extern “C” in C++?
- What exactly is the difference between “pass by reference” in C and in C++?
- What is `CString`?
- Difference between != and =! with an example(in C++)
- Unresolved external symbol in object files
- How do sizeof(arr) / sizeof(arr[0]) work?
- rand() between 0 and 1
- how to implement Interfaces in C++?
- What exactly is nullptr?
- How to make a SIMPLE C++ Makefile
- 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]
- Expected initializer before function name
- Inheriting constructors
- Pass a vector by reference C++
- lvalue required as left operand of assignment – Array
- What is uintptr_t data type
- Why do we use volatile keyword? [duplicate]
- C++ round a double up to 2 decimal places
- Windows 7 exception code: 0xc0000409
- Convert float to std::string in C++
- “items list” or “item list”
- When and why do I need to use cin.ignore() in C++?
- What does int & mean
- Evaluate a string with a switch in C++ [duplicate]
- terminate called after throwing an instance of ‘std::out_of_range’
- What does “missing template argument” mean?
- undefined reference to ‘std::cout’
- warning: control may reach end of non-void function
- Error at constructor : Expected an identifier?
- identifier “string” undefined?
- What does LPCWSTR stand for and how should it be handled with?
- A warning – comparison between signed and unsigned integer expressions
- How can I convert const char* to string and then back to char*?
- Difference in make_shared and normal shared_ptr in C++
- VC++ fatal error LNK1168: cannot open filename.exe for writing
- Reading integers from file and store them in array C++ [closed]
- Uninitialised value was created by a stack allocation
- C++ Image Processing Libraries
- Printing the correct number of decimal points with cout
- error C2011: ” : ‘class’ type redefinition
- error: request for member ‘..’ in ‘..’ which is of non-class type
- Socket API or library for C++?
- Initialize empty vector in structure – c++
- C++ catching all exceptions
- error: new types may not be defined in a return type
- error: passing ‘const …’ as ‘this’ argument of ‘…’ discards qualifiers
- Deleting an object in C++
- Appending a vector to a vector
- C++ Expression must have pointer-to-object type
- C++ “Access violation reading location” Error
- Getting error: ISO C++ forbids declaration of with no type
- IntelliSense: the object has type qualifiers that are not compatible with the member function
- codingbat-like site for C++
- extra qualification error in C++
- RPN Calculator for C++
- How can I get the list of files in a directory using C or C++?
- How do you make a HTTP request with C++?
- Error: C2228: left of ” must have class/struct/union
- error C2679: binary ‘<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
- Function definition not found for a function declared inside unnamed namespace – how to resolve? (Visual Studio 2015)
- How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?
- How do I get the type of a variable?
- What is the meaning of a C++ Wrapper Class?
- terminate called after throwing an instance of ‘std::out_of_range’ what(): basic_string::substr
- error C2106: ‘=’ : left operand must be l-value
- What does it mean that “a declaration shadows a parameter”?
- error C2601: ‘main’ : local function definitions are illegall – MS VS 2013 Compiler
- std::string length() and size() member functions
- How to convert QString to int?
- Update GCC on OSX
- Which is faster C++ String length() or size()?
- creating an array of structs in c++
- How to read lines of text from file and put them into an array
- Undefined reference to class constructor, including .cpp file fixes
- Why can’t we pass arrays to function by value?
- multiple definitions error in c++ and solution to solve this issue
- Function stoi not declared
- How to draw line in OpenGL?
- c++ sizeof( string )
- Error: member function may not be declared outside of its class.
- Struct inheritance in C++
- How do I simply compare characters in C++?
- How to Check the Version of my gcc?
- Difference between function arguments declared with & and * in C++
- C++ floating point to integer type conversions
- How to get an average in C++?
- C++: variable ‘std::ifstream ifs’ has initializer but incomplete type
- template argument 1 is invalid (Code::Blocks Win Vista) – i don’t use templates
- invalid use of template name without an argument list
- What is “error C2061: syntax error : identifier “?
- Link error “undefined reference to `__gxx_personality_v0′” and g++ [duplicate]
- How to use bitmask?
- -Error reading characters of string