2019年10月第三周总结

1. 关于tuple

tuple是模板,tuple所有成员都是public的
使用tuple的相关操作,需要包含头文件。
希望将数据组合成单一对象时,使用tuple非常有用。(快而随意的数据结构)
一个tuple可以有任意数量的成员,tuple的成员类型也不相同。
一个确定的tuple类型的成员数目是固定的,意味着不能有添加和删除能够改变成员数目的操作
#include <iostream>
#include <tuple>
using namespace std;
int main ()
{
    tuple<char, int, float> geek;
    geek = make_tuple('a', 10,15.5);
    cout << "The initial values of tuple are:";
    cout << get<0>(geek) <<" " << get<1>(geek);
    cout << " " << get<2>(geek) << endl;
    
    get<0>(geek) = 'b';
    get<2>(geek) = 20.5;
    
    cout << "The modefied values of tuple are:";
    cout << get<0>(geek) << " " << get<1>(geek);
    cout << " " << get<2>(geek) << endl;
    
    return 0;
}

2. 关于enum

#include <bits/stdc++.h>
using namespace std;
int main()
{
    enum Gender{Male, Female
    };
    
    Gender gender = Male;
    switch(gender){
        case Male:
            cout << "Gender is Male";
            break;
        case Female:
            cout << "Gender is female";
            break;
        default:
            cout << "Gender is male or female";
    }
    return 0;
}

3. 关于map

#include <iostream>
#include <string>
#include <map>
int main ()
{
  std::map<std::string,int> mymap = {                { "alpha", 0 },                { "beta", 0 },                { "gamma", 0 } }; 
 mymap.at("alpha") = 10;
  mymap.at("beta") = 20;  
mymap.at("gamma") = 30; 
 for (auto& x: mymap) 
{    std::cout << x.first << ": " << x.second << '\n';  } 
 return 0;
}
/*输出:
alpha: 10
beta: 20
gamma: 30
*/

猜你喜欢

转载自www.cnblogs.com/zhaijiayu/p/11707109.html