STL : vector & string

string

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

int main(int argc, char ** argv) {

	string s1("heLLo");
	string s2(s1);
	string s3(6, 'c');  // s3:cccccc

	s1.empty();   // 空返回true,否則返回false
	string::size_type sz = s1.size();    // 返回字符個數

	/***************************字符串讀取方式**************************/
	string s;
	while (cin >> s){          // 按空格讀取
		cout << s << endl;
	}

	while (getline(cin, s)){  // 按行讀取
		if (!s.empty()){
			cout << s.size() << ":" << s << endl;
		}
	}
	/***************************字符串遍歷**************************/\
	unsigned int count = 0;
	for (auto c : s1 ){
		if (isupper(c)){   // 加入頭文件cctype
			count++;
		}
	}
	cout << count << endl;

	/***************************修改字符串**************************/\
	for (auto &c : s1) {         // 修改字符串中的字符需要傳入引用
		if (isupper(c)) {        // 加入頭文件cctype
			c = tolower(c);
		}
	}
	cout << s1;

	system("pause");
	return 0;
}

vector



	vector<int> a(10, 1);
	vector<int> b(a);
	vector<int> c{5,10,15,20,25 };


	vector<int> v4(10);       // 10個元素,值爲0
	vector<int> v5{ 10 };       // 1個元素
	vector<string> v6{ 10 };  //10個元素,空字符串


	/**********************查找屬性********************/
	a.empty();                                 // 空,返回true
	vector<int>::size_type sz = a.size();      // 當前容器內存放的元素個數
	vector<int>::size_type cp = a.capacity();  // 返回向量中總共可容納的元素數目
	a.resize(3, 2);                            // 設置size的大小
	a.reserve(30);                             // 設置capacity的大小
	int first = a.front();                     // 返回頂部元素
	int last = a.back();                       // 返回尾部元素
	a[2];
	a.at(2);
	/***********************增加元素*******************/
	a.push_back(4);                      // 尾插
	vector<int>::iterator it1 = a.begin();
	vector<int>::iterator it2 = a.end();
	advance(it1, 3);                     // 迭代器指針前移3次
	a.insert(it1, 5);                    //在迭代器位置插入5
	a.insert(it1, 3, 5);                   //在迭代器位置插入3個5
	/***********************刪除元素*******************/
	a.pop_back();
	a.erase(it1);                        // 刪除單個元素
	a.erase(it1, it2);                    // 刪除之間的所有元素
	a.clear();                           // size爲0,存儲空間不變
	///***********************修改元素*******************/
	b.assign(a.begin(), a.end()); // 拷貝a
	b.assign(6, 7);               // 刪除所有結點並對b重新賦值
	a.swap(b);                    // 交換兩鏈表
	///***********************打印鏈表*******************/
	for (vector<int>::iterator it1 = a.begin(), it2 = a.end(); it1 != it2; ++it1) {
		cout << *it1 << " ";
	}

	vector<int>v = { 1,2,3,4 };
	for (auto &i : v) {
		i *= i;
	}
	for (auto it1 = v.begin(), it2 = v.end(); it1 != it2; it1++) {
		cout << *it1 << " ";
	}


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