《N诺机试指南》(三)STL使用

1.vector

2.queue

3.stack

4.map

5.set

6.多组输入输出问题

详解见代码以及注释:

//学习STL的使用 
#include <bits/stdc++.h>
using namespace std;

int main(){
//	1.使用vector
//	vector<int> v;//也可以vector<string类型> 
//	for(int i=0; i<=10; i++){
//		v.push_back(i*i);//加入到vector中去 
//	}
//	for(int i=0; i<v.size(); i++){//范围0~v.size() 
//		cout << v[i] << " ";//访问vector元素 
//	}
//	cout << endl; 
	
//	2.使用queue 
//	queue<int> q;//定义一个空队列
//	//先进先出 
//	q.push(1);//入队 
//	q.push(2);
//	q.push(3); 
//	while( !q.empty() ){//队列不为空 
//		cout << q.front() << " ";//取出队首元素
//		q.pop();//出队 
//	}

//	3.使用stack栈
//	stack<int> s;
//	//后进先出 
//	s.push(1); 
//	s.push(2);
//	s.push(3);
//	while( !s.empty() ){
//		cout << s.top() << " ";
//		s.pop();
//	}

//	4.使用map(key-value)键值对
//	map<string, int> dict;//定义一个map
//	//key:string类型,value:int类型
//	dict["tom"] = 1;//定义映射关系 
//	dict["jack"] = 2;
//	dict["xiaxia"] = 6; 
//	if( dict.count("xiaxia") ){//如果存在"xiaxia",count("xiaxia")=true 
//		cout << "xiaxia is very" << dict["xiaxia"]; 
//	} 
//	//使用迭代器遍历map的key和value
//	map<string, int>::iterator it;//定义迭代器 
//	for(it=dict.begin(); it!=dict.end(); it++){
//		cout << it->first << ":" << it->second << endl;
//	}
//	dict.clear();//清空map 
	
//	5.使用set
//	set<string> s;
//	s.insert("安徽"); 
//	s.insert("陕西");
//	s.insert("北京");
//	s.insert("上海");
//	set<string>::iterator it;
//	for(it=s.begin(); it!=s.end(); it++){
//		cout << *it << " ";
//	}
//	cout << endl;
//	s.erase("北京");
//	s.erase("上海");
//	for(it=s.begin(); it!=s.end(); it++){
//		cout << *it << " ";
//	}
//	cout << endl;
//	if( s.count("陕西") ){
//		cout << "陕西 I'm coming!" << endl; 
//	} 
//	s.clear();//清空set 

//	6.多组输入输出问题
//	//6.1:C语言版
//	int a, b;
//	while( scanf("%d %d", &a, &b)!=EOF ){
//		printf("%d", a+b);
//	}
//	//6.2:C++语言版
//	int a, b;
//	while( cin >> a >> b ){
//		cout << a+b << endl;
//	} 
		
	return 0;
} 

补充:多组输入输出问题:

 

 

 

posted @ 2020-02-16 14:01  Kimishima-Kana  阅读(247)  评论(0编辑  收藏  举报