Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MagTDK

Easy STL question

This topic is 5852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This should be an easy question to answer. For some reason I can''t get it to work. How can I declare a function to accept a stl list or vector as a parameter of that function. I want it so I can still retain all of the functionality of a list or vector within that function.

Share this post


Link to post
Share on other sites
Advertisement
You mean like this?


    
#include <vector>

#include <list>

#include <algorithm>

#include <iostream>

#include <iterator>

using namespace std;

template<typename T>
void func(const T& v)
{
copy(v.begin(), v.end(), ostream_iterator<T::value_type>(cout, "\n"));
}

int main()
{
vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(2);

func(v);

list<double> l;
l.push_back(0.0);
l.push_back(1.1);
l.push_back(2.2);

func(l);
}


[edited by - SabreMan on September 14, 2002 4:52:08 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I feel compelled to point out that in 99% of cases, you don''t actually need/want your function to take the container (list/vector). Rethink your algorithm, and decide if you can get by with a pair of iterators. Look at the algorithms in STL -- tons of them, and not a single one requires a container to be passed in. There are advantages to working this way -- each of the iterator abstractions are very well defined, and your code will support things like C arrays via pointers. Of course, I could be wrong, you might need the container, but I highly doubt it. Something to think about...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!