這裏記錄一些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

 

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