With its complex syntax, Pointers, memory management, generic programming and other features, C++ has baffled a large number of IT students. Xiaobian to CSharp entry, midway to C++ when the pain over a period of time, but with the business ability of progress, has gradually fallen in love with this powerful, flexible language.
Here are some learning experiences to share
How to learn C++ how to learn C++, this question is no use talking, or you need to use your eyes, hands and brains. There are three steps:
learn
Learn, whether it’s reading a book, watching a video, or taking a training course, at least systematically and consistently. Personal advice if you have the conditions to attend training is the best, if not can watch video learning. But either way, I recommend reading and learning from books. No matter you are training or watching videos, your mind is following others. Only when you read a book can you have your own understanding and be more profound.
C++ : I heard C++ is hard to learn? How to learn C++?
copy
Copy, here means to copy the code on the tutorial or half key half copy, at least the project through their own hands, and can run normally. This process is very painful. Many people may feel that they can learn knowledge, but when it comes to their key, they are short of money. Even when copying debug, there are still a lot of mistakes. This period of time is the time to doubt life, we must insist, insist, insist.
For example, the simplest HelloWord, file reading, standard stream I/O, etc
int main()
{
printf("hello word") ;
cout<<"hello word";
int a ;
cin>>a
}
Copy the code
write
Such as skilled to a certain degree, you need to independently implement a small function or develop a small project, which will encounter a lot of problems, you can check materials, check the teaching materials, consulting others, etc.. When you are able to implement the function you want to implement independently, then congratulations you have started, and it will be more and more smooth.
Summary of C++ knowledge difficulties
Here is a summary of some C++ learning difficulties
Pointer to the
We’ve talked a lot about Pointers. To understand Pointers, you need to understand the concepts of computer memory and memory locations, as well as the * and & operators.
Figurative understanding:
Memory is land, memory address is land number, and when our program creates variables and objects, the computer acts as the planning bureau and allocates a numbered piece of land for variables and objects, which is called memory, and the number is called memory address. Land has a use period (70 years), after the period of recovery, variables and objects in the computer will also be released when the memory is recycled.
Pointer: A pointer itself is a variable, similar to an int or float, except that int holds 10, 146, etc., while pointer variables hold the address of memory.
The * operator: retrieves objects stored at memory addresses.
The & operator: takes the address of the memory in which the object resides.
Pointer to: We often say that the so-and-so pointer points to, meaning that this pointer variable holds the address of that object in computer memory.
C++ : I heard C++ is hard to learn? How to learn C++?
Pointer diagram
Generic programming
C++ generics are implemented based on templates. Neither a class template nor a function template is a real class or function, just a blueprint that the compiler uses to generate code.
Why generic programming
As a simple example, suppose you implement a linked list class that can be used to dynamically store ints. But now there is a requirement to store string data dynamically. What do you do? Implement another class for a linked form that stores string data? This is obviously against code reuse, and you might wonder if you can pass in data types as parameters. That’s where C++ generic programming comes in;
A function template
The type of the data can also be passed as an argument. If a function can be defined without specifying a specific data type, the compiler will automatically infer the data type from the arguments passed in when the function is called, thus implementing the template of the function. The dummy type is used as a placeholder for an identifier, and the real type is inferred back from the arguments passed in when the function is called.
void change(T& a,T& b)
{
T temp = a;
a = b ;
b = temp ;
}
Copy the code
The above code implements a generic template function that swaps two numbers, with type T being the placeholder identifier. This template implements a function that can exchange two parameters of any data type.
Test the
int a = 10,b = 20 ; change(a,b); cout<<a<<"|"<<b; string c = "i love u",d = "i love u too"; change(c,d); cout<<c<<"|"<<d; Either template<typename T> or template<class T> can be used, and there can be multiple parameters of type T: template<typename T1, typename T2..... >Copy the code
Class template
Type parameters defined in class templates can be used in class declarations and class implementations. The purpose of a class template is also to parameterize the type of data.
template<class T1,typename T2>
class MyClass
{
public:
MyClass(T1& a ,T2& b):a_(a),b_(b){} ;
T1& Func1();
void Func2(T2& x);
private:
T1 a_;
T2 b_ ;
};
template<class T1,typename T2>
T1& MyClass<T1,T2>::Func1()
{
T1 temp = this->a_ ;
return &temp ;
}
template<class T1,typename T2>
void MyClass<T1,T2>::Func2(T2& x)
{
this->b_ = x ;
}
Copy the code
STL
C++ generics were originally developed for STL, which contains templates for common data structures (such as linked lists, variable-length arrays, sorted binary trees) and algorithms (such as sorting, lookup). It is a very powerful and useful library.
The container implementation in STL is based on the class template. Algorithms such as sorting, search algorithms are implemented on the basis of function templates.
C++STL containers are divided into sequential containers and associative containers. Sequential containers include: variable length dynamic array vector, dual-ended queue deque, bidirectional list list. Their elements depend on the order of position, not the size of the elements. Associative containers include set, multiset, map, and multimap. The elements in an associative container are sorted, and the order of the elements is directly related to the elements themselves, with no physical order.
Any container comes with iterator functions, which are:
Begin () : Returns an iterator to the first element in the container.
End () : Returns an iterator that refers to the position after the last element in the container.
Rbegin () : Returns a reverse iterator to the last element in the container.
Rend () : Returns a reverse iterator to the position before the first element in the container.