which STL container do I need for storing a 2D-array?

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
B

BobR

Martin Jørgensen wrote in message
There's a small difference compared to programming in C, isn't there?

I mean: In C I think the following is possible:

void func(){
int abc, bla, blabla;

blabla = 5;
abc = 0;

for( bla=0; bla< blabla; bla++){
printf("Hi");
abc++; /* abc is in scope in C, I think - am I right? */
/* abc is not in scope in C++, or is it??? */

std::cout << " abc=" << abc << std::endl; // no prob.
}
}

So I think abc isn't in scope in C++... But I'm not sure... I've
debugged a little in C++... Is it right or wrong?
Best regards
Martin Jørgensen

#include <iostream>
#include <ostream>

int main(){
int abc(0), bla(0), blabla(5);
std::cout << abc << " " << bla << std::endl;
int xyz(5);
cout <<"xyz="<<xyz<< std::endl;

for( int bla(0); bla < blabla; ++bla){
++abc;
std::cout << " abc=" << abc << std::endl;
std::cout << " bla=" << bla << std::endl;
int xyz(25); // it's inside a 'scope' {}. A separate object.
std::cout <<" xyz="<<xyz<< std::endl;
} // for(bla)

std::cout << abc << " " << bla << std::endl;
std::cout <<"xyz="<<xyz<< std::endl;

return 0;
} // main()

(like 'Vegas) In C++, what's in a for() stays in the for()! <G>
Note how there is an 'int bla' in main(), and *another* 'int bla' in the
for(), and no name clash.

/* -- output --
0 0
xyz=5
abc=1 // <--- Hmmm, seems to be 'in scope'
bla=0
xyz=25
abc=2
bla=1
xyz=25
<snip>
abc=5
bla=4
xyz=25
5 0 // <--- 'abc' changed, but, 'bla' is original
xyz=5 // <--- the outside 'xyz' is the same
*/

Does that clear up any 'C' to 'C++' cobwebs?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,264
Latest member
FletcherDa

Latest Threads

Top