These two days have been learning STL, first roughly talk about my way of learning: the first is to roughly see the STL's introductory book, and then go through the online Daniel Video, while following the operation, do not understand the general on the Internet has been looking for! Look at the post of Daniel! The basic learning method is this, in general, no one can avoid the actual operation of this link, because we are to learn from the application! Maybe someone learning new knowledge faster, you can not watch the video and so on, I just want to say that their own situation to decide it! No nonsense, I am only a small beginner, talk about these two days encountered problems!
Daniel's video, every time to include the required header files, and then is to choose the appropriate container, initialization, output, and then the corresponding method of the narration! Basically is such a process, here is not to say that Daniel's teaching method has a problem, this absolute science, because each lesson tells the content is not very strong, and if someone is a beginner just want to know some chapters, then this method is suitable for any one! Daniel always said, can also deepen the memory, Just started to learn to listen to this is really good! But later, because these operations are more conventional, the basic has mastered, is the speed of typing is not the rhythm of Daniel, so in these ordinary operation to pause, keep up the rhythm, the heart of a little bit uncomfortable! So, I just thought about initializing the data operation and the printout operation, written as a template function, put in a custom header file, and then call it directly! Here's how:
By the way, there's a little bit of emphasis here, it's really cool, but it feels like someone has used it, and there should be some way! I know, here the knowledge points involved in C + + program compilation, the writing and invocation of the header file , and so on, but when you have to write out the time to pay attention to, it is necessary to pay attention to, it is usually learning to form a good learning habits, and not just to write to see! These knowledge point everyone to go to science, the park has danale explanation, I also collected a few! We are interested to see!
Here's a summary of the issues that need attention in this way:
- C + + language support "compile separately" what the hell is going on?
- Header file writing generally only put the declaration, but there are special circumstances, when the call to prevent repeated calls , specifically how to write, etc.
- The concept of absolute paths and relative paths may be encountered when writing header files when calling
Program function: Take container vector as an example, realize inserting 1~9 data, and output:
The main functions are as follows:
1#include <algorithm>2#include <functional>3#include <iterator>4 //the method of relative path, including the custom header file5#include".. \.. \include\smalltools.h"6 7 using namespacestd;8 9 intMain ()Ten { Onevector<int>Ivec; A -InitData (Ivec,1,9); - the print (IVEC); - - return 0; -}
The specific implementation of the header file:
1 #ifndef Wahaha2 #defineWahaha3 //Use header file protectors to prevent duplicate inclusions4#include <iostream>5#include <vector>6 7Template <typename elementtype>8 voidPrint (elementtype&elem)9 {Ten for(Elementtype::iterator iter = Elem.begin (); ITER! = Elem.end (); + +iter) One { Acout << *iter <<" "; - } -cout <<Endl; the } - -Template <typename elementtype> - voidInitData (elementtype& Elem,intBinte) + { - //Initialize Insert Data + for(inti = b; I <= e; ++i) A { at Elem.push_back (i); - } - } - - #endifimplementation of the header file
Because the template function is involved here, the implementation of the function method is also put in directly! I also think that if only learning, in the Baohan of the header file will be commonly used in several container header files are written in, the main function of the province to write, because we are beginners to write the program is relatively small and not particularly complex! This is really convenient, but the heart always feel strange ...
Program run results and file distribution:
All right, basically, that's what you want to say! If there is a problem with the above description, please comment below, thank you!!!
STL Learning article: "Opportunistic" to learn