c++ 11 tuple使用,与vector set结合

原文:https://oopscenities.net/2015/09/10/stdtuple/

#include <iostream>
#include <set>
#include <string>
#include <tuple>
 
usingnamespace std;
 
usingcar = tuple<string, string, int>;
 
voidprint(constcar& c)
{
    cout << get<0>(c) << ", " << get<1>(c) << "; " << get<2>(c) << endl;
}
 
intmain()
{
  set<car> cars;
  cars.emplace("Toyota","Rav4", 2012);
  cars.emplace("VW","Jetta", 2015);
  cars.emplace("Chevrolet","Sonic", 2013);
  cars.emplace("BMW","X5", 2014);
  cars.emplace("VW","Jetta", 2014);
   
  for(auto& i : cars)
    print(i);
 
  cout << "******"<< endl;
  autoit = cars.find(car { "Toyota","Rav4", 2012 });
  if(it == cars.end())
    cerr << "CAR NOT FOUND" << endl;
  else
    print(*it);
     
  return0;
}

intmain()
{
  vector<tuple<int, string>> vec;
  vec.push_back(make_tuple(10,"ten"));
  vec.emplace_back(20,"twenty");
  vec.emplace_back(30,"thirty");
 
  for(auto& i : vec)
  {
     cout << "("<< get<0>(i) << "; " << get<1>(i) << ")"<< endl;
  }
}



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页