C++ has two keywords that can be used to define new types: typedef and using. Both of these keywords allow you to create a new type name that can be used to declare variables, but they work in slightly different ways.
typedef is an older keyword that has been part of the C++ language since the beginning. It is used to create a new type name that is an alias for an existing type. For example, you could use typedef to create a new type named MyInt that is an alias for the built-in int type:
Syntax
typedef int MyInt;
After this definition, you can use MyInt to declare variables just like you would use int:
Snippet
MyInt x = 5;
Using is a newer keyword that was introduced in C++11. It works in a similar way to typedef, but it has a more flexible syntax and can be used in more places in your code. For example, you can use using to define a new type name in the same way as typedef:
Snippet
using MyInt = int;
This creates a new type named MyInt, which is an alias for the built-in int type. You can then use MyInt to declare variables just like you would use int.
In general, using is considered to be a more modern and flexible way to define new type names in C++. typedef is still supported for backward compatibility, but the newest code should use using instead.
Here is an example of using the using keyword to define a new type named MyInt that is an alias for the built-in int type:
C++ code (Example-1)
#include <iostream>
// Define a new type named MyInt that is an alias for int
using MyInt = int;
// The main driver code functionality ends from here
int main()
{
// Declare a variable of type MyInt
MyInt x = 5;
// Print the value of the variable
std::cout << "x = " << x << std::endl;
return 0;
// The main driver code functionality ends from here
}
This code will print the following output:
Note that using is not limited to defining type aliases for built-in types like int. You can use it to define type aliases for any type, including user-defined types, template types, and more.
C++ code (Example-2)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Create a vector of integers with an initial capacity of 5 elements
vector<int> numbers(5);
// Add some elements to the vector
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
numbers.push_back(40);
numbers.push_back(50);
// Print the size and capacity of the vector
cout << "Size: " << numbers.size() << endl;
cout << "Capacity: " << numbers.capacity() << endl;
// Remove the last element from the vector
numbers.pop_back();
// Print the size and capacity of the vector after removing an element
cout << "Size: " << numbers.size() << endl;
cout << "Capacity: " << numbers.capacity() << endl;
// Clear all elements from the vector
numbers.clear();
// Print the size and capacity of the vector after clearing
cout << "Size: " << numbers.size() << endl;
cout << "Capacity: " << numbers.capacity() << endl;
return 0;
}
Output:
Size: 10
Capacity: 10
Size: 9
Capacity: 10
Size: 0
Capacity: 10
In this code, we create a 'vector' object that can hold a sequence of integers. We then add some elements to the vector using the 'pushback' method and print its size and capacity. Next, we remove the last element from the vector using the 'popback' method and print its size and capacity again
C++ code (typedef example)
#include <iostream>
#include <vector>
using namespace std;
// Create an alias for a vector of integers
typedef vector<int> IntVector;
int main()
{
// Create a vector of integers using the alias
IntVector numbers;
// Add some elements to the vector
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
// Print the elements of the vector
for (IntVector::iterator it = numbers.begin(); it != numbers.end(); ++it)
{
cout << *it << " ";
}
return 0;
}
Output:
10 20 30
In this code, we use the 'typedef' keyword to create an alias called 'IntVector' for a 'vector' of integers. We then use this alias to create a 'vector' object and add some elements to it.