这里记录一些c++中string新特性的一些用法

/////这里是string的使用方式
string one("lotty winner");
	cout << one << endl;


	string tow(20, '$');
	cout << tow << endl;

	string three(one);
	cout << three << endl;


	one += "hellow one";
	cout << one << endl;

	three[0] = 'p';
	cout << three << endl;

	string four;
	four = one + tow;
	cout << four << endl;

	char alll[] = "ALL s well that ends well";
	cout << alll << endl;

string five(alll, 10);
	cout << five << endl;

	string six(alll + 2, alll + 10);
	cout << six << endl;

	string seven(&five[6], &five[8]);
	cout <<"seven: "<< seven << endl;

	string eight(four, 7, 2);
	cout << eight << endl;


///使用初始化列表的方式

initializer_list<char> pontchar = { '3','4','7' };

	string charstr(pontchar);
	cout << charstr << endl;
	for (auto itm : charstr)
	{
		cout << itm << endl;
	}







这里看一下使用初始化列表的类.

//这里看一个使用初始化列表的类.
class AutoPtr
{
public:
	AutoPtr(std::initializer_list<char> listPtr):listP(listPtr)
	{
		
	}
	void showData()
	{
		for (auto outstr : listP)
		{
			cout << outstr << " ";
		}
		cout << endl;
	}

private:

	int Number;
	char* strName;
	std::initializer_list<char> listP;
};

//使用方式.
	AutoPtr tmep{'u','i','p'};
	tmep.showData();

这里看一下智能感知类型   decltype 

char  te1 = 0;
	decltype(te1) te2 =12 ;
	cout << te2 << endl;

	//测试一下引用.
	const int k = 11;
	decltype(k*2) k2 = k;
	cout <<"k+1: "<< k2 << endl;

这里再开一下指针的使用情况

//测试一下指针.
	const int* ptrk = &k;
	decltype(ptrk) ptrk2 = ptrk;
	cout << *ptrk2 << endl;

	//常量指针和指针常量.
	int a = 10;
	int b = 12;
	constMethod(&a,&b);
	cout << "a: " << a << " b: " << b << endl;


	//又回到了原始的总结,要想在某一个函数里面,达到修改数值的目的,必须要做到
	//存在差级.....


	int* k222 = &a;
	cout << " 函数之前a的地址: " << k222 << "  tempa的地址: " << &tempa << endl;

	constMethod2(&k222, &b);
	cout << " 函数之后a的地址: " << *k222 << "  tempa的地址: " << &tempa << endl;

	cout << "a: " << a << " b: " << b << endl;

#pragma endregion

#pragma region 函数模板的问题.

   auto tt =eff(10, 20);
	cout << "类型.tt: " << tt << endl;

#pragma endregion




	

这里看一下array 

initializer_list<int> listsI{1,3,4,5,6};
	for (auto & tek : listsI)
	{
		cout << tek << " ";
	}
	cout << endl;

	vector<int> vecInt = {10,44,34,5345,5};
	for (auto &veck : vecInt)
	{
		veck = 989;
		cout << veck << " ";

	}
	cout << endl;
	array<int, 10> artemp = {10,20,30,};
	//artemp.fill(99);
	for (auto art : artemp)
	{
		cout << art << " ";
	}
	cout << endl;
	
	cout << "first element: " << get<0>(artemp) << endl;
	for (int i = 0; i < artemp.max_size(); i++)
	{
		cout << get<1>(artemp) << " ";
	}
	cout << endl;


	std::array<int, 3> myarray = { 10, 20, 30 };
	std::tuple<int, int, int> mytuple(10, 20, 30);

	std::tuple_element<0, decltype(myarray)>::type myelement;  // int myelement

	myelement = std::get<2>(myarray);
	std::get<2>(myarray) = std::get<0>(myarray);
	std::get<0>(myarray) = myelement;

	std::cout << "first element in myarray: " << std::get<0>(myarray) << "\n";
	std::cout << "first element in mytuple: " << std::get<0>(mytuple) << "\n";

	cout << endl;

这里看一下 truple 元组和 decltype 组合使用的问题

#pragma region 元组
	auto tup1 = std::make_tuple("first", "second", 23, 't', 355);
	for (int ik =0;ik< tuple_size<decltype(tup1)>::value;ik++)
	{

		cout << get<0>(tup1) << " ";
	}
	cout << endl;

	auto tup2 = std::forward_as_tuple(1, "3344");
	cout << get<1>(tup2) << endl;

#pragma endregion

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章