Click here to Skip to main content
15,883,705 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: another "everybody knows that " question on C++ Pin
OriginalGriff12-May-23 23:32
mveOriginalGriff12-May-23 23:32 
GeneralRe: another "everybody knows that " question on C++ Pin
jschell12-May-23 9:36
jschell12-May-23 9:36 
QuestionMessage Closed Pin
8-May-23 6:28
Member 149687718-May-23 6:28 
AnswerRe: C++ for loop syntax ? Pin
CPallini8-May-23 8:02
mveCPallini8-May-23 8:02 
AnswerRe: C++ for loop syntax ? Pin
Richard MacCutchan8-May-23 21:46
mveRichard MacCutchan8-May-23 21:46 
GeneralMessage Closed Pin
9-May-23 7:13
Member 149687719-May-23 7:13 
GeneralRe: C++ for loop syntax ? Pin
Richard MacCutchan9-May-23 10:11
mveRichard MacCutchan9-May-23 10:11 
GeneralRe: C++ for loop syntax ? Pin
k50549-May-23 11:29
mvek50549-May-23 11:29 
You should be thinking in terms of containers, elements and iterators. While the ranged for loop does work for POD arrays, the dimension has to be known at compile time. For example
C++
void showarray(int *arr)
{
   for(i : arr) 
        std::cout << i << ' ';
   std::cout << '\n';
}
will not compile, since the dimension of arr is not known. On the other hand, if the compiler can deduce the size of the array, then a ranged for loop may be used with a POD array:
C++
#include <iostream>

int main()
{
    std::cout << "Enter size : ";
    int n;
    std::cin >> n;
    int arr[n];
    for(int i = 0; i < n; ++i) {
        arr[i] = i;
    }
    for( auto x : arr) {
        std::cout << x << ' ';
   }
    std :: cout << '\n';
}
Here the compiler can deduce the dimension of the array arr and this program compiles and executes as expected. In the general case, though the ranged for loop works with containers which implement begin() and end(). That's true for STL containers, and lots of other container like objects from other libraries.

Addendum In addition to begin() and end() the container also needs to implement iterators. In the STL, you cannot use a ranged for loop with a stack, queue or priority_queue
Keep Calm and Carry On


modified 10-May-23 13:40pm.

AnswerRe: C++ for loop syntax ? Pin
Gerry Schmitz10-May-23 5:02
mveGerry Schmitz10-May-23 5:02 
AnswerRe: C++ for loop syntax ? Pin
jschell10-May-23 5:34
jschell10-May-23 5:34 
QuestionTitle bar display and closing problem Pin
Jeanjean Lesage5-May-23 22:46
Jeanjean Lesage5-May-23 22:46 
AnswerRe: Title bar display and closing problem Pin
Richard MacCutchan6-May-23 0:03
mveRichard MacCutchan6-May-23 0:03 
GeneralRe: Title bar display and closing problem Pin
Jeanjean Lesage6-May-23 1:57
Jeanjean Lesage6-May-23 1:57 
QuestionCreating a pointer and asigning a variable address to it Pin
Calin Negru23-Apr-23 6:22
Calin Negru23-Apr-23 6:22 
AnswerRe: Creating a pointer and asigning a variable address to it Pin
Gerry Schmitz23-Apr-23 8:58
mveGerry Schmitz23-Apr-23 8:58 
AnswerRe: Creating a pointer and asigning a variable address to it Pin
Victor Nijegorodov23-Apr-23 20:14
Victor Nijegorodov23-Apr-23 20:14 
AnswerRe: Creating a pointer and asigning a variable address to it Pin
Richard MacCutchan23-Apr-23 21:26
mveRichard MacCutchan23-Apr-23 21:26 
GeneralRe: Creating a pointer and asigning a variable address to it Pin
Calin Negru23-Apr-23 22:39
Calin Negru23-Apr-23 22:39 
GeneralRe: Creating a pointer and asigning a variable address to it Pin
harold aptroot23-Apr-23 21:49
harold aptroot23-Apr-23 21:49 
GeneralRe: Creating a pointer and asigning a variable address to it Pin
CPallini25-Apr-23 20:27
mveCPallini25-Apr-23 20:27 
QuestionMessage Closed Pin
22-Apr-23 6:51
Member 1496877122-Apr-23 6:51 
AnswerRe: How to implement "\n" in QT QDebug class Pin
k505422-Apr-23 7:32
mvek505422-Apr-23 7:32 
GeneralMessage Closed Pin
22-Apr-23 13:00
Member 1496877122-Apr-23 13:00 
GeneralRe: How to implement "\n" in QT QDebug class Pin
Richard MacCutchan22-Apr-23 21:10
mveRichard MacCutchan22-Apr-23 21:10 
Questionget text of SysListView32(LVS_OWNERDATA+LVS_OWNERDRAWFIXED) items from other applications Pin
Ilqar Sadiqov8-Apr-23 9:02
Ilqar Sadiqov8-Apr-23 9:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.