June 15th, 2009 10:58 AM
#1
use of vector<int>::value_type?
Hi,
I wonder what is the use of value_type?
If I type
vector<int>::value_type i;
i is an int right? But I had to specifiy the int in vector<int> anyway. So what is the use of that?
I could have done
int i;
instead. I guess I am missing something.
Thank you!
PS: ok I guess this comes in to play when I get to templates.
Last edited by w3rd; June 15th, 2009 at 11:02 AM .
June 15th, 2009 11:07 AM
#2
Re: use of vector<int>::value_type?
Originally Posted by
w3rd
ok I guess this comes in to play when I get to templates.
Yes, I believe that is the idea, e.g., if the container type is T, you could get the value type as typename T::value_type.
June 15th, 2009 11:12 AM
#3
Re: use of vector<int>::value_type?
what do you mean by container type T? Like vector or list?
vector::value_type wouldnt make much sense, would it?
Well I will be patient still some hundred pages until templates...
June 15th, 2009 11:14 AM
#4
Re: use of vector<int>::value_type?
value_type is used in templated functions and classes where it is the only way to determine the type used by a template parameter.
Code:
template <typename T>
T::value_type Function(const T &t)
{
T::value_type i;
...
return i;
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
June 15th, 2009 11:17 AM
#5
Re: use of vector<int>::value_type?
Originally Posted by
w3rd
what do you mean by container type T? Like vector or list?
vector::value_type wouldnt make much sense, would it?
Say, std::vector<int>, std::list<double>, std::vector<long, some_allocator_type>, etc.
By the way, JohnW@Wessex's example is missing the typename keyword before typename T::value_type .
June 15th, 2009 11:19 AM
#6
Re: use of vector<int>::value_type?
value_type and similar typedefs are useful for writing generic code that can work with any STL container. Eg:
Code:
#include <vector>
#include <list>
#include <iostream>
template<typename Container>
class Foo
{
Container m_con;
public:
Foo(const Container& c) : m_con(c) { }
typename Container::value_type
GetFirst( ) { return *(m_con.begin( )); }
};
int main( )
{
std::vector<int> a;
a.push_back(20);
std::list<float> b;
b.push_back(1.234f);
Foo<std::vector<int> > c(a);
Foo<std::list<float> > d(b);
std::cout << c.GetFirst( ) << std::endl;
std::cout << d.GetFirst( ) << std::endl;
return 0;
}
June 15th, 2009 11:25 AM
#7
Re: use of vector<int>::value_type?
So JohnW: your Funtion() would be used like
Code:
std::vector<int> ivec;
std::vector<int>::value_type result = Function(ivec)
right? But it would work for different sequential containers with different value_types.
ok thank you both.
I am seeing it like through a mist now
Will get there.
OK one last question. Is there a difference between:
std::vector<int>::value_type search_value;
and
int search_value; ?
Last edited by w3rd; June 15th, 2009 at 11:39 AM .
June 15th, 2009 12:45 PM
#8
Re: use of vector<int>::value_type?
std::vector<int>::value_type search_value;
and
int search_value; ?
No, they're both ints. Outside of templates the main reason to use value_type would be with typedefs. If you use something like
Code:
typedef std::vector<int> DataVector;
DataVector vec;
DataVector::value_type value;
cin >> value;
vec.push_back(value);
Then if you needed to change the type of data later on, say from int to double, you would only need to change the typedef.
June 15th, 2009 12:59 PM
#9
Re: use of vector<int>::value_type?
Hey Speedo, thanks! I thought that it could be used to do that but I could not get a hold of how exactly. I tried
which didnt work.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks