#include
#include
#include
#include
#include
using namespace std;
class SreBob
{
public:
SreBob();
SreBob(initializer_list il);//大括号初始化的调用的函数
int size()const { return data->size(); }
bool empty()const { return data->empty(); }
void push_back(const string &t)
{
data->push_back(t);
}
void pop_back();
string &front();
string &back();
~SreBob();
private:
std::shared_ptr<vector> data;
void check(int i, const string &msg) const;
};
SreBob::SreBob() :data(std::make_shared <vector>())
{
}
SreBob::SreBob(initializer_list il) : data(std::make_shared <vector>(il))
{
}
void SreBob::pop_back()
{
check(0, “pop_back on empty SreBob”);
data->pop_back();
}
string & SreBob::front()
{
check(0, “front on empty SreBob”);
return data->front();
}
string & SreBob::back()
{
check(0, “back on empty SreBob”);
return data->back();
}
SreBob::~SreBob()
{
}
void SreBob::check(int i, const string &msg) const
{
if (i >= data->size())
{
throw out_of_range(msg);//自定义异常和使用
}
}
std::tuple<char, int, string> gettuple()
{
return std::make_tuple(‘a’, 456, “bcdefg”);
}
template<class T,class U>
auto addT(T a, U b)
{
return a + b;
}
int main()
{
SreBob b1;
try
{
b1.pop_back();
}
catch (std::out_of_range &exp)
{
cout << exp.what() << endl;
}
SreBob b2{ “123”,“456”,“789” };
b1 = b2;
b1.push_back(“abc”);
cout << b1.size() << endl;
cout << b2.size() << endl;
//c++越来越像python了
for (auto x : { 1,2,3,4,5,6 })
{
cout << x << " ";
}
cout << endl;
vector vec2{ 1,5,9,8,6,3,7,4,2 };
for (vector::iterator iter = vec2.begin(); iter != vec2.end(); iter++)
{
cout << *iter << " ";
}
cout << endl;
//很强大的模板类推导
auto a = addT(78, 45.0);
cout << a << endl;
system("pause");
return 0;
}