What we bring to you today are some of the knowledge points of the C + + extension standard library! This is part of the update, and another part will be updated later!
OK, today we say another gadget, this tool will have some unexpected effects when dealing with some complex data, but it has to be said that I am a standard library faithful, so I write the program basically will prioritize the library, and then the living Boost library, And the next thing I'm going to do is expand with the standard library stuff.
Since it is an extension of the standard library, the first step is to simplify some of the things in the standard library, of course, just to make it easier for us to use when debugging the program.
The standard output input stream for C + + provides an operator (<<) for ease of use for output (>>) input, so for simplicity, we can first put the standard containers inside C + + into this streaming operation, such as:
//====================================
Std::vector v;
v<<1<<2<<3<<4.......; Press the data into the container
int a,b,c,d;
v>>a>>b>>c>>d; Extracts the elements in the container from the back to the front
//======================================
It's not a good thing to make a container that easy, but if you get used to this kind of programming habit you'll find that it's nice to do the rest, OK, so the first step is to implement the function first.
To implement the above features everyone may have thought of the first step, perhaps the following:
//=====================================
Template
Inline std::vector& operator<< (std::vector& V, const t& value) {
V.push_back (value);
return v;
}
Template
Inline std::vector& operator>> (std::vector& V, t& value) {
if (V.empty ()) {
Value = T ();
}
else{
Value = V.back ();
V.pop_back ();
}
return v;
}
//========================================
Indeed, we can do this and work very well, if we want to operate a list, then we will implement a list version of the can:
//========================================
Template
Inline std::list& operator<< (std::list& L, const t& value) {
L.push_back (value);
return l;
}
Template
Inline std::list& operator>> (std::list& L, t& value) {
if (L.empty ()) {
Value = T ();
}
else{
Value = L.back ();
L.pop_back ();
}
return l;
}
//==========================================
OK, this is the list of streaming operations also come out, then if the container is a queue ... Well, even if the whole writing is not much, because the standard library inside the container is actually so few, but if there are many ...
Well, I do not know you can also think of the previous to tell you about the template of some advanced techniques no, if you can't think of, it's okay, you can go back to see, about 90 about, if you are already familiar with that is better, but because we can use a simpler way to achieve this simple operation, That is, using template parameters, first of all we can certainly know whether it is a vector or a list, or deque they all have a common feature, is to use push_back to press the data into the container, using the Pop_back function to eject the data from the back, So we only need two functions to be able to complete the flow of these three standard containers, as follows:
//==========================================
Template class C>
c& operator<< (c& C, const t& value) {
C.push_back (value);
return C;
}
Template class C>
c& operator>> (c& C, t& value) {
if (C.empty ()) {
Value = T ();
}
else{
Value = C.back ();
C.pop_back ();
}
return C;
}
//==========================================
OK, the use of templates is so convenient, here we are using template template parameters this technique. Here is the implementation of the set:
//===========================================
Template class S>
s& operator<< (s& c,const t& value) {
C.insert (value);
return C;
}
Template class S>
s& operator>> (s& C, t& value) {
if (C.empty ()) {
Value = T ();
}
else{
Auto it =--c.end ();
Value = * (it);
C.erase (IT);
}
return C;
}
//================================================
Because the set and the vector do not have the same operation, the notation is slightly different and the map is similar to set:
//===============================================
Template
Template class M>
m& operator<< (m& M, const std::p air& p) {
M.insert (P);
return m;
}
Template
Template class M>
m& operator>> (m& M, std::p air& p) {
if (M.empty ()) {
p = Std::make_pair (K (), V ());
}
else{
Auto it =--m.end ();
p = *it;
M.erase (IT);
}
return m;
}
//================================================
OK, here we basically have some of the standard library of some commonly used containers have been packaged, then we want to debug the program when we encounter a container, we want to know what is inside the container, for convenience, we may want to be able to manipulate the container like the basic type of operation, such as:
//=================================================
Std::vector v;
V << 1 << 2 << 3 << 4 << 5;
std::cout<
//==================================================
To achieve this function is as simple as above, today we use this to do the end, the next we continue:
//===================================================
Printing of the Vector,list deque
Template class C>
std::ostream& operator<< (std::ostream& os, const c& C) {
if (C.empty ())
return OS;
Std::copy (C.begin (),--c.end (), Std::ostream_iterator (OS, ","));
OS << * (--c.end ());
return OS;
}
//==================================================
The following is the printing of set
//==================================================
Template class S>
std::ostream& operator<< (std::ostream& os, const s& c) {
if (C.empty ())
return OS;
Std::copy (C.begin (),--c.end (), Std::ostream_iterator (OS, ","));
OS << * (--c.end ());
return OS;
}
//==================================================
Print to map
//=================================================
Template
Template class M>
std::ostream& operator<< (std::ostream& os, const m& M) {
if (M.empty ())
return OS;
Std::for_each (M.begin (), M.end (), [&] (std::p air p) {
OS << p.first << "," << p.second << ",";
});
return OS;
}
//=================================================
C + + Tutorial extending the standard library part One