STL source code learning

1. STL<sstream> library: use string instead of character array to avoid buffer overflow; incoming parameters and return value types can be automatically deduced to avoid type conversion errors

istream, ostream and stringstream perform stream input, output and input and output operations respectively; stringstream construction and destructor consume CPU time, it is best to reuse, clear() to clear

string s = "100000";

int n = 0;

stream << s;

stream >> n; // n equals 100000.

template <class T>
void to_string(string &res, const T &t) {
  ostringstream oss;
  oss << t;
  res = oss.str();
}
The getline() function in the sstream library 
istream& getline (char* s, streamsize n ); istream& getline (char* s, streamsize n, char delim );

Extracts characters from the stream as  unformatted input  and stores them into  s  as a c-string, until either the extracted character is the  delimiting character , or  n  characters have been written to  s  (including the terminating null character). const string until the extracted characters are delimited, such as '\n', or n characters have been stored (including '\0').

 1 #include<iostream>
 2 #include<sstream>
 3 using namespace std;
 4 
 5 // main function
 6 int main() {
 7   stringstream ss_stream;
 8   int first, second;
 9   ss_stream << "456";
10   ss_stream >> first;
11   cout << first << endl;
12   // 注意clear()清除管道已有数据
13   ss_stream.clear();
14   ss_stream << true;
15   ss_stream >> second;
16   cout << second << endl;
17 
18   ss_stream.clear();
19   ss_stream << "字符串一" << endl;
20   ss_stream << "字符串二" << endl;
21   ss_stream << "字符串三" << endl;
22   ss_stream << "字符串四" << endl;
23 
24 //  char buffer[100];//25declare uninitialized
//   while (ss_stream.getline(buffer, sizeof(buffer))) {
26 //    printf("msg=%s\n", buffer);
27 //  }
28 
29   string stemp;
30   while (getline(ss_stream, stemp)) {
31     cout << stemp << endl;
32   }
33   return 0;
34 }

 

2. C++ class template template <class T>

Templates are the basis of generic programming, classes are the abstraction of objects, templates are the abstraction of classes, and concrete classes can be defined with templates. It belongs to compile-time polymorphism, which is reflected in the polymorphism of parameters.

Use: template <class T> // class template with parameter T

1 #include<iostream>
 2 #include<cmath>
 3  using  namespace std;
 4  
5  // the declaration of class template 
6  template< class T> 
7  class Point {
 8   public :
 9    Point(T = 0 , T = 0 ) ;   // Class constructor 
10    Point(Point&);         // Class copy constructor 
11    T Distance(Point&);    // Member function of return type T 
12   private :
 13    T x, y;
 14 };
15 
16 // The implementation of class template Point
17 template <class T>
18 Point<T>::Point(T a, T b) : x(a), y(b){}
19 
20 template <class T>
21 Point<T>::Point(Point& a) {
22   x = a.x;
23   y = a.y;
24 }
25 
26 template <class T>
27 T Point<T>::Distance(Point &a) {
28   return sqrt((x - a.x)*(x - a.x) + (y - a.y) * (y - a.y));
29 }
30 
31 // main function
32 int main() {
33   Point<int> a1(3, 4), a2(5, 6);
34   cout << "The distance of these two points: "<< a1.Distance(a2) << endl;
35   
36   Point<double> b1(7.8, 9.8), b2(34.8, 25.4);
37   cout << "The distance of these two points: "<< b1.Distance(b2) << endl;
38   return 0;
39 }

 

3、cmath是c++语言中的库函数,其中的c表示函数是来自c标准库的函数,math为数学常用库函数。编译时必须加上参数「-lm」。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324787246&siteId=291194637