小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C pair的定義及使用

 fisher60 2012-11-23

C++ pair的定義及使用

聲明為pair類型的變量可以有三種賦值方法:

1. 通過初始化賦值 直接聲明的時候 后面加括號并且數(shù)據(jù),如下a
2. 通過.first .second 來賦值 如下b
3. 通過 = make_pair() 來賦值, 如下c
  a. pair <string,double> product1 ("tomatoes",3.25);
  pair <string,double> product2;
  pair <string,double> product3;
 b.  product2.first = "lightbulbs"; // type of first is string 
    product2.second = 0.99; // type of second is double


 c. product3 = make_pair ("shoes",20.0);


template <class T1, class T2> struct pair;
Pair of values

This class couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through the public members first and second.

The class is defined as:

1
2
3
4
5
6
7
8
9
10
11
12
template <class T1, class T2> struct pair
{
  typedef T1 first_type;
  typedef T2 second_type;

  T1 first;
  T2 second;
  pair() : first(T1()), second(T2()) {}
  pair(const T1& x, const T2& y) : first(x), second(y) {}
  template <class U, class V>
    pair (const pair<U,V> &p) : first(p.first), second(p.second) { }
}


Members

first_type, second_type
Alises of template parameters T1 and T2 respectively.
first, second
Data members containing the first and second values stored in the pair.
pair()
Constructs a pair object with each of its members first and second constructed with their respective default constructors.
pair(const T1& x, const T2& y)
Constructs a pair object with its members first and second initialized to x and y, respectively.
template <class U, class V> pair (const pair<U,V> &p)
Constructs a pair object with its members first and second initialized to the corresponding elements in p, which must be of any couple of implicitly-convertible types (including the same types).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <utility>
#include <string>
using namespace std;

int main () {
  pair <string,double> product1 ("tomatoes",3.25);
  pair <string,double> product2;
  pair <string,double> product3;

  product2.first = "lightbulbs";     // type of first is string
  product2.second = 0.99;            // type of second is double

  product3 = make_pair ("shoes",20.0);

  cout << "The price of " << product1.first << " is $" << product1.second << "\n";
  cout << "The price of " << product2.first << " is $" << product2.second << "\n";
  cout << "The price of " << product3.first << " is $" << product3.second << "\n";
  return 0;
}


Output:

The price of tomatoes is $3.25
The price of lightbulbs is $0.99
The price of shoes is $20

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多