【9-4】知识点小结

1.使用transform方法实现大小写的转换:

#include <bits/stdc++.h>
using  namespace std;
int main(){
	string str = "aBCdEFgHiJK";
	string temp_upper = str;
	string temp_lower = str;
	transform(temp_upper.begin(),temp_upper.end(),temp_upper.begin(),::toupper);
	transform(temp_lower.begin(),temp_lower.end(),temp_lower.begin(),::tolower);
	cout << "str = " << str << endl;
	cout << "temp_upper = " << temp_upper << endl;  
	cout << "temp_lower = " << temp_lower << endl;
	return 0;
}

2.ccf考试不可以使用itoa因为提交无法识别该非通用库函数,但是可以使用atoi和atof

3.stringstream实现数据转换大全:

#include <bits/stdc++.h>
using  namespace std;
int main(){

	std::stringstream stream;//定义类stringstream的一个对象 stream 
	std::string str;//定义一个string类对象 str 

	//int 到 string 
	int num = 123456789;
	stream << num; //数据入流 
	stream >> str; //数据出流 
	cout << "str = " << str << endl;
	
	//string 到 int
	int temp;
	string str_num = "123456789";
	stream.clear(); //清空流 
	stream << str_num;
	stream >> temp;
	cout << "temp = " << temp << endl; 
	
	//int 到 char* 
	char str1[10];
	stream.clear();
	stream << num;
	stream >> str1;
	cout << "str1 = " << str1 << endl;
	
	//string 到 char* 
	stream.clear();
	reverse(str.begin() , str.end());
	cout << "reverse_str = " << str << endl;
	stream << str;
	stream >> str1;
	cout << "transfer str1 = " << str1 << endl;
	
	//char* 到 string(不使用stringstream类)
	for(int i = 0 ; i < strlen(str1) ; i++){
		str1[i] = '0';
	} 
	cout << "str1 = " << str1 << endl;
	str = str1;
	cout << "str = " << str << endl;
	
	//double 到 string
	double temp_dou = 123.4567;
	stream.clear();
	stream << temp_dou;
	stream >> str;//会出现精度丢失 
	cout << "double to string = " << str << endl;
	
	//string 到 double
	string str_dou = "123.456";
	double dou_result;
	stream.clear();
	stream << str_dou;
	stream >> dou_result;
	cout << dou_result << endl;
	 
	//float 到 string
	float floatNum = 12.34;
	stream.clear();
	stream << floatNum;
	stream >> str; 
	cout << "str = " << str << endl;
	
	//string 到 float
	string str_float = "567.77";
	stream.clear();
	stream << str_float;
	stream >> floatNum;
	cout << "floatNum = " << floatNum << endl;
	 
	return 0;
} 

4.map知识点复习

#include <bits/stdc++.h>
using namespace std;
int main(){
	map <string , int> m;
	m["xi"] = 1;
	if(m.count("i")){
		cout << m["i"] << endl;
	}
	else{
		cout << "i is not a legal key" << endl;
	}
	if(m.count("xi")){
		map <string,int> :: iterator it = m.find("xi");
		m.erase(it);
	}
	//注意find函数查找的是key值而不是value值 
	cout << "size = " << m.size() << endl;
	cout << "max_size = " << m.max_size() << endl;
	m["a"] = 1;
	m["b"] = 2;
	cout << "size = " << m.size() << endl;
	m.clear();
	cout << "size = " << m.size();
	return 0;
} 

5.set知识点复习

#include <bits/stdc++.h>
using namespace std;
int main(){
	set <int> S;
	for(int i = 0 ; i < 100 ; i++){
		S.insert(i);
	}
	if(S.count(1)){
		cout << "1 is in it" << endl; 
		S.erase(1);
		cout << "1 is now not in it" << endl;
	}
	else{
		cout << "1 isn't in it" << endl;
	}
	cout << "size = " << S.size() << endl;
	for(set <int> :: iterator it = S.begin() ; it != S.end() ; it++){
		cout << *it << " ";
	}
	return 0;
}

6.queue知识点复习

#include <bits/stdc++.h>
using namespace std;
int main(){
	queue <int> q;
	q.push(1);
	q.push(2);
	q.push(3);
	q.pop();
	while(!q.empty()){
		q.pop();
	}
	cout << q.size() << endl;
	cout << "back = " << q.back() << endl;
	cout << "front = " << q.front() << endl;
	return 0;
}

7.stack知识点复习

using namespace std;
stack <int> S;
int main(){
//	S.push(1);
//	S.push(3);
//	S.push(11);
//	cout << S.top() << endl;
//	while(!S.empty()){
//		S.pop();
//	}
	//括号匹配问题
	stack <char> S; 
	int n;
	cin >> n;
	char temp_ch;
	int times = 0;
	while(cin >> temp_ch){
		times++;
		if(temp_ch == '('){
			S.push(temp_ch);
		}
		else{
			if(!S.empty()){
				S.pop();
			}
			else{
				cout << "illegal";
				return 0;
			}
		}
		if(times == n){
			break;
		}
	}
	if(!S.empty()){
		cout << "illegal";
		return 0;
	}
	cout << "legal!" << endl; 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/chenhanxuan1999/article/details/82384935
今日推荐