Jump to content
  • Advertisement


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


Easy STL question

This topic is 5934 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
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;


list<double> 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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!