Does MC++ have foreach?

E

Edward Yang

I am testing VS 2005 Express August CTP.

I am very thrilled by the fact that I can write code agaist the .NET
Framework (in new syntax). Now I have a question - I want to enumerate
all elements in a collection. In C# or VB, I'd use foreach or For Each.
But classic C/C++ does not have such a keyword. I want to know if there
is a workaround or template function?
 
C

Carl Daniel [VC++ MVP]

Edward said:
I am testing VS 2005 Express August CTP.

I am very thrilled by the fact that I can write code agaist the .NET
Framework (in new syntax). Now I have a question - I want to enumerate
all elements in a collection. In C# or VB, I'd use foreach or For
Each. But classic C/C++ does not have such a keyword. I want to know
if there is a workaround or template function?

C++/CLI (as implemented in VS2005) includes for each (two words) - use it
just like you would in C#

See http://msdn2.microsoft.com/library/ms177202(en-us,vs.80).aspx

-cd
 
N

Nishant Sivakumar

You can use "for each" in native apps too [it's a non-standard feature you'd
find pretty handy]
 
N

Nishant Sivakumar

In case that's not clear, here's some code (compiled without /clr) :-

#include <iostream>
#include <vector>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
vector<int> vecint;
vecint.push_back(100);
vecint.push_back(500);
vecint.push_back(200);
for each(int i in vecint)
cout << i << endl;
}

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com
http://blog.voidnish.com


Nishant Sivakumar said:
You can use "for each" in native apps too [it's a non-standard feature
you'd find pretty handy]

--
Regards,
Nish [VC++ MVP]


Edward Yang said:
Thanks. It's amazing!
 
E

Edward Yang

Thanks again for your code. I did not realize that it also works for
native code. That's amazing (again). :)
 

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

Top