20
votes
4answers
9k views

Is it possible to declare two variables of different types in a for loop?

Is it possible to declare two variables of different types in the initialization body of a for loop in C++? For example: for(int i=0,j=0 ... defines two integers. Can I define an int and a char in ...
15
votes
3answers
549 views

What is the advantage of using universal references in range-based for loops?

const auto& would suffice if I want to perform read-only operations. However, I have bumped into for (auto&& e : v) // v is non-const a couple of times recently. This makes me wonder: ...
50
votes
11answers
24k views

Can I use break to exit multiple nested for loops?

Is it proper to use the break function to exit several nested for loops? If so, how would you go about doing this? Can you also control how many loops the break exits?
36
votes
3answers
11k views

C++11: how to use range-based for() loop with std::map?

The common example for C++0x range-based for() loops is always something simple like this: std::vector<int> numbers = { 1, 2, 3, 4, 5, 6, 7 }; for ( auto xyz : numbers ) { std::cout ...
11
votes
1answer
1k views

Range-based for statement definition redundancy

Looking at n3092, in ยง6.5.4 we find the equivalency for a range-based for loop. It then goes on to say what __begin and __end are equal to. It differentiates between arrays and other types, and I find ...
22
votes
7answers
3k views

Should I use std::for_each?

I'm always trying to learn more about the languages I use (different styles, frameworks, patterns, etc). I've noticed that I never use std::for_each so I thought that perhaps I should start. The goal ...
16
votes
5answers
685 views

Can I declare variables of different types in the initialization of a for loop?

Why does this C++ code not compile under VS2010: for ( int a = 0, short b = 0; a < 10; ++a, ++b ) {} while this one does: short b = 0; for ( int a = 0; a < 10; ++a, ++b ) {} Is the ...
3
votes
4answers
3k views

C++ for-loop - size_type vs. size_t

In the C++ Primer book, Chapter (3), there is the following for-loop that resets the elements in the vector to zero. for (vector<int>::size_type ix = 0; ix ! = ivec.size(); ++ix) ivec[ix] = 0; ...
22
votes
6answers
19k views

How to put two increment statements in a C++ 'for' loop?

I would like to increment two variables in a for-loop condition instead of one. So something like: for(int i = 0; i != 5; ++i and ++j) do_something(i,j); What is the syntax for this?
15
votes
4answers
2k views

How to write a `for` loop over bool values (false and true)

A question mostly for fun/curiosity: how to write a for loop in C++ that would iterate over two values of a bool (i.e. true and false), using only operations with bool (i.e. without conversions to ...
7
votes
8answers
1k views

Performance issue for vector::size() in a loop

hi when having a vector<int> var; for(int i=0; i< var.size();i++) , is the size() function called each time or only once ? from the answers I guess I better use iterators , or just have ...
4
votes
4answers
455 views

What does “for(;;)” mean?

In C/C++, what does the following mean? for(;;){ ... } Unfortunately I can't Google that and get meaningful results.
2
votes
10answers
5k views

++i or i++ in for loops ?? [duplicate]

Possible Duplicate: Is there a performance difference between i++ and ++i in C++? Is there a reason some programmers write ++i in a normal for loop instead of writing i++?
35
votes
31answers
3k views

format of for loops

i'd just like to get some thoughts on the best way to do for loops (in c based languages). all over the web code samples have for loops which look like this for(int i = 0; i < 5; i++) while i ...
14
votes
3answers
32k views

How to loop through a c++ map [duplicate]

Possible Duplicate: how to iterate in reverse over a map in c++ How would I loop through a map in c++ i've searched but none seem to work for me. My map is defined as follows std::map< ...

1 2 3 4
15 30 50 per page