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
C++

Avatar of undefined
Last Comment
evilrix

8/22/2022 - Mon
elimesika

HI


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

is exactly as

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

ASKER
lzha022

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
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> > >.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Infinity08

>> 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
evilrix

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
lzha022

Hi Evilrix,
Thanks for the answer. Yes, i did not put <string> after vector.
Regards,
Alison
elimesika

This was exactly what I said .....
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
evilrix

>> 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

ASKER
lzha022

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?
evilrix

>> 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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23