Link to home
Start Free TrialLog in
Avatar of lzha022
lzha022

asked on

why use typedef vector<string> std_vec;

Hello experts,
I used to program in Java, and now i am doing c++ programming. I have the following code
...
typedef vector<string> my_vec;

void read(const char* fileName, my_vec& my_vec)
{
...
}

Here i do not understand why it does not directly use std::vector for function read()? I tried this
void read(const char* fileName, vector& my_vec)
{
...
}
It gave me a compile error. Could anyone explain why?
Regards,
Alison
Avatar of elimesika
elimesika
Flag of Israel image

HI


void read(const char* fileName, my_vec& my_vec)
{
...
}

is exactly as

void read(const char* fileName, vector<string>& my_vec)
{
...
}

Avatar of lzha022
lzha022

ASKER

But i get this compile error if i use the second one.
c:\Documents and Settings\lzha022\Desktop\make\read\read.cpp(20): error C2955: 'std::vector' : use of class template requires template argument list
Avatar of Infinity08
>> Here i do not understand why it does not directly use std::vector for function read()?

Because it might be easier to understand my_vec than vector<string> ... This is a very simple example, so the advantage of using a typedef might not be obvious, but take a look at a container like this for example :

        std::map<std::string, std::vector<pair<double, std::string> > >

Instead of having to type all that out, it's easier to just create a typedef :

        typedef std::map<std::string, std::vector<pair<double, std::string> > > MyMap;

so that MyMap becomes a sort of alias for that container. And from then on you can use MyMap instead of std::map<std::string, std::vector<pair<double, std::string> > >.
>> c:\Documents and Settings\lzha022\Desktop\make\read\read.cpp(20): error C2955: 'std::vector' : use of class template requires template argument list

Can you show the exact code you used ? (preferrably the whole file)
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lzha022

ASKER

Hi Evilrix,
Thanks for the answer. Yes, i did not put <string> after vector.
Regards,
Alison
This was exactly what I said .....
>> Thanks for the answer. Yes, i did not put <string> after vector.
No worries, do you now understand the reason it is required

Another reason typedefs are use is they facilitate generic programming. You can typedef a template parameter of a class into the class itself to make it available outside the class without the consuming code needing to know the actual type. The type is polymorphic but defined at compile time so it is statically polymorphic. A good example of this is the value_type typedef in a vector. It's easier to show via a contrived example than to explain I think :)


#include <iostream>
 
template <typename T>
class A
{
public:
	A():m_value(0){}
 
	// value_type will be different depending on what type T is (vector does this too)
	typedef T value_type;
 
	T const & get() const { return m_value; }
 
private:
	T m_value;
};
 
 
template <typename T>
typename T::value_type foo(T const & t) // The return type is statically polymorphic
{
	return t.get();
}
 
int main()
{
	A<int> a1;
	int i = foo(a1);
 
	A<char> a2;
	char c = foo(a2);
}

Open in new window

Avatar of lzha022

ASKER

Hi Evilrix,
Thank you for the exmaple. But i find itvery hard to understand.
Is the following a function?
typename T::value_type foo(T const & t) // The return type is statically polymorphic
{
      return t.get();
}

The other day, i saw a question like to implement pre-imcrement and post-increment operator for this class
public class foo()
{

}
I guess it should be done by using template, but i have no idea how to do this. Could you please show me how to do this?
>> Is the following a function?
Yes, it's a template function. The type T is different depending upon how it's called. Because of this the return type can only be defined by using the value_type of type T, which will be different when called with different types of T. Of course, type T must model this concept (the type must present a type value_type) otherwise it wont work.

>> The other day, i saw a question like to implement pre-imcrement and post-increment operator for this class
>> I guess it should be done by using template, but i have no idea how to do this. Could you please show me how to do this?
They don't need to be done using templates no, but that's a different Q, unrelated to this, so you should open it as a new post.