set练习

时间:2023-03-09 14:20:52
set练习
#include <iostream>
#include <set>
#include <vector> using namespace std; int main()
{
set<int> setInt;
vector<int> ivec;
for(vector<int>::size_type i = 0; i != 11; ++i)
ivec.push_back(i);
setInt.insert(ivec.begin(), ivec.end());
setInt.insert(11);
set<int>::iterator itor1 = setInt.find(9);// 这个也对,为什么const类型的键值可以用非const的迭代器?????
set<int>::const_iterator itor2 = setInt.find(10);
if(itor1 != setInt.end())
cout << *itor1 << endl;
/*
*itor1 = 12; //error:keys in a set are read-only 正如不能修改mao中元素的键部分一样,set中的键也是const,在获取迭代器后只能对其进行读操作
*/
if(itor2 != setInt.end())
cout << *itor2 << endl;
system("pause");
return 0;
}

  

 #include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <set>
#include <map> using namespace std; void restricted_wc( vector<string> strVec, map<string, int> &wordCount )
{
// create a set of excluded words
set<string> excluded;
for ( vector<string>::iterator it = strVec.begin(); it != strVec.end(); ++it )
{
excluded.insert( *it );
} string word;
cout << " Input some words to count the words in wordCount(map) ( ctrl + z to end): "
<< endl;
cin.clear();
while ( cin >> word )
{
if ( !excluded.count( word ) )
++wordCount[ word ];
}
} int main(int argc, char* argv[])
{
cout << " Input some words to vector<string> excluded ( ctrl + z to end): " << endl;
vector<string> excludedVec;
string excludedWord;
while ( cin >> excludedWord )
excludedVec.push_back( excludedWord ); // use restricted_wc()
map< string, int > wordCount;
restricted_wc( excludedVec, wordCount ); // show out the map:wordCount
cout << "\n\tShow out the map:wordCount: " << endl;
for ( map< string, int >::iterator it = wordCount.begin(); it != wordCount.end(); ++it )
{
cout << " The word '" << (*it).first << "' appears for " << (*it).second << " times. " << endl;
} system("pause");
return ;
}

随机推荐

  1. java php c# 三种语言的AES加密互转

    java php c# 三种语言的AES加密互转 最近做的项目中有一个领取优惠券的功能,项目是用php写得,不得不佩服,php自带的方法简洁而又方便好用.项目是为平台为其他公司发放优惠券,结果很囧的是 ...

  2. rabbitmq的发布确认和事务 - 2207872494的个人空间

    rabbitmq的发布确认和事务 - 2207872494的个人空间   https://my.oschina.net/lzhaoqiang/blog/670749

  3. JSP Servlet学习笔记——使用fileupload上传文件

    关键代码如下: index.jsp <body> <center> <h3>文件上传</h3> <font color="red&quo ...

  4. W3wp.exe占用CPU及内存资源

    问题背景 最近使用一款系统,但是经常出现卡顿或者用户账号登录不了系统.后来将问题定位在了服务器中的“w3wp.exe”这个进程.在我们的用户对系统进行查询.修改等操作后,该进程占用大量的CPU以及内存 ...

  5. How to Set Up DTrace to Detect PHP Scripting Problems on Oracle Linux

    http://www.oracle.com/technetwork/articles/servers-storage-admin/php-dtrace-linux-2062229.html

  6. LINUX 内核守护进程

    http://alfred-sun.github.io/blog/2015/06/18/daemon-implementation/

  7. 使用cat读取和echo写内核文件节点的一些问题

    span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror ...

  8. 移植Python3到TQ2440(一)

    平台 硬件:TQ2440  64MB内存 256MB NandFlash bootloader:U-Boot 2015.04 kernel:linux-4.9 Python: Python-3.6.0 ...

  9. C#中泛型容器Stack<T>的用法,以及借此实现”撤销/重做”功能

    .Net为我们提供了众多的泛型集合.比如,Stack<T>先进后出,Queue<T>先进先出,List<T>集合元素可排序,支持索引,LinkedList<T ...

  10. WordPress主题开发:WP_Query常用参数

    常用参数 用途 调用文章或页面 s 查询和某个关键词相关的所有的文章/页面信息 p 文章或页面id post__in 多篇id post__not_in 多篇id以外 post_type 查询的信息类 ...