Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 For Each - why i cant do this?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
matt271




PostPosted: Fri May 06, 2011 6:32 pm   Post subject: For Each - why i cant do this?

why i cant do this??

c++:
#include <iostream>
#include <vector>
using namespace std;

// why cant i do this?
template <class T>
void forEath(T v) {
    T::iterator i;
    for (i = v.begin(); i != v.end(); i++)
        cout << *i << endl;
}

// i thought maybe this, but still no?
template <class T, N>
void forEath(T<N> v) {
    T<N>::iterator i;
    for (i = v.begin(); i != v.end(); i++)
        cout << *i << endl;
}

int main() {
    vector<int> v;
    v.push_back(10);
    v.push_back(50);
    v.push_back(1);
    v.push_back(2);
    v.push_back(5);
   
    // this works
    vector<int>::iterator i;
    for (i = v.begin(); i != v.end(); i++) {
        cout << *i << endl;
    }
   
    // here i try to call the first one
    forEath(v);
   
    // here i try to call the next one
    forEath<vector<int>>(v);
   
   
    return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
Alexmula




PostPosted: Sat May 07, 2011 2:08 pm   Post subject: RE:For Each - why i cant do this?

for the first forEach put a typename before T::iterator and it should work. check out this link http://pages.cs.wisc.edu/~driscoll/typename.html
matt271




PostPosted: Sat May 07, 2011 5:48 pm   Post subject: Re: For Each - why i cant do this?

very cool. ty for that link very helpful
matt271




PostPosted: Sat May 07, 2011 8:09 pm   Post subject: Re: For Each - why i cant do this?

reading more and playing around i have come up with this:

c++:
#include <iostream>
#include <vector>
using namespace std;

template <typename T, typename F>
void forEath(T v, F f) {
    //typename T::iterator i;
    for (auto i = v.begin(); i != v.end(); i++) {
        f(*i);
    }
}

int main() {
    vector<int> v;
    v.push_back(10);
    v.push_back(11);
    v.push_back(12);
    v.push_back(13);
   
    forEath(v, [](int& i)->void{
        cout << i << endl;
    });

    return 0;
}


compile with -std=c++0x flag

i think its cool to create a better "for each" in c++0x. i would like to define some macros and stuff to make it even cleaner. also use "where" to make it safer. anybody want to help?

my overall goal is to be able to code in c++ (or c++0x) using the shortcuts java has spoiled me with.

i would like it to look more like this
c++:

for_each (int i, v) {
    cout << i << endl;
}

or even
c++:

for_each (const T& i, v) {
    cout << i << endl;
}


to be like javas
Java:

for (int i : v) {
    System.out.println(i);
}
Insectoid




PostPosted: Sat May 07, 2011 9:52 pm   Post subject: RE:For Each - why i cant do this?

Better to learn to use C++ as intended rather than try to make it Java. It's a different style that you should learn to use effectively (loving it is a bit much).
matt271




PostPosted: Sun May 08, 2011 9:21 am   Post subject: RE:For Each - why i cant do this?

good point
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: