c++关联容器的容器操作(和顺序容器都支持的操作)详细解释,基础于c++primer 5th 表 9.2 (持续更新)

关联容器和顺序容器都是容器,所以都支持容器操作。就是表格9.2所示的操作。以下以map为例测试表格中的操作是否满足关联容器。

1.类型别名:

#include <iostream>
#include <string>
#include <map>
#include <set>
#include <iterator>
#include <fstream>
using namespace std;

int main()
{
string f = "file";
ifstream in(f);
map<string,unsigned> words_count;
string str;
while(in >> str)
{
words_count[str] ++;
}

//iterator
map<string,unsigned>::iterator it;
for(it = words_count.begin(); it != words_count.end();++it)
{
cout << it->first << " occurs " << it->second << (it->second > 1?"times":"time") << endl;
}
//const_iterator
map<string,unsigned>::const_iterator it1 = words_count.cbegin();
while(it1 !=words_count.end())
{
cout << it1->first << " occurs " << it1->second << (it1->second > 1 ? "times":"time")<<endl;
++it1;
}
//reverse_iterator
map<string,unsigned>::const_reverse_iterator it3;
for(it3 = words_count.crbegin();it3 != words_count.crend();++it3)
cout << it3->first << " occurs " << it3->second << (it3->second > 1?"times":"time")<< endl;

//size_type
map<string,unsigned>::size_type s1;
s1 = words_count.size();
cout << "words_count 's size is:" << s1 << endl;

//difference_type
map<string,unsigned>::difference_type d;
//d = words_count.end() - words_count.begin();
//cout << d << endl;
/*这里报错,gcc编译器支出,这里不支持-运算符,说明迭代器不能相减*/

//value_type
for(map<string,unsigned>::value_type i : words_count)
cout << i.first << endl;

//reference or const_reference
for(map<string,unsigned>::const_reference i : words_count)
	cout << i.first<< " occurs " << i.second << " times. " << endl;

return 0;
}

文件file是一个存放数据的文件.

运行全部通过。

总结,g++编译器不支持关联容器的迭代器的 -  减法运算符。其余表格9.2里面的map都支持。set情况大家自己可以实验。

2.构造函数

#include <iostream>
#include <string>
#include <map>
#include <set>
#include <fstream>
using namespace std;
int main()
{
string f= "file";
ifstream in(f);
map<string,unsigned> words_count;
set<string> words;
string str;

while(in >> str)
{
  words_count[str] ++;	   
}
in.close();
in.open(f);

//下面测试构造函数set先
set<string> s1{"str1","str2","str3","str4","str5","str6","str7"};
set<string> s2;
set<string> s3(s1);
set<string> s4(s1.begin(),s1.end());

//再看看map
map<string,int> m1;
while(in >>str)
{
m1[str] ++;
}
map<string,int> m2(m1);
map<string,int> m3(m1.begin(),m1.end());
map<string,int> m4;

   return 0;
}

总结:对于构造函数

C c;   C  c1(c2);      C   c(c1.begin(),c1.end());   C  c{p1,p2,p3...};都成立,就是map的列表初始化还没学到。这里没法实验。

3.赋值与swap

#include <iostream>
#include <string>
#include <map>
#include <set>
#include <fstream>
using namespace std;
int main()
{
string f= "file";
ifstream in(f);
map<string,unsigned> words_count;
set<string> words;
string str;
while(in >> str)
{
  words_count[str] ++;	   
}

//map ' s assignment
  // from ==
map<string,unsigned> m1;
m1 = words_count;

//set 's assignment
  // from {}
set<string> s1 = {"ab","ac","ad","efc"};
  // from = 
set<string> s2 = s1;
s2 = {"robert","liming","Wiliam"};
for(set<string>::const_reference i : s2)
	cout << i << endl;

   return 0;
}

 关于swap例子:

#include <string>
#include <iostream>
#include <map>
#include <set>
using namespace std;
int main()
{
map<string,int> m1;
map<string,int> m2;
m1.swap(m2);
    return 0;
}

4.c.size(),c.max_size(),c.empty(),c.clear()都满足

#include <string>
#include <iostream>
#include <set>
#include <map>
using namespace std;
int main()
{
string str;
map<string,int> words_cnt;
if(words_cnt.empty())
        cout << "words_cnt is empty!!!" << endl;
while(cin >> str)
{
  words_cnt[str] ++;
}
cout << words_cnt.size() << endl;
cout << words_cnt.max_size() << endl;
return 0;
}
~    

运行结果如下: 

words_cnt is empty!!!
asd fasd fasdf asdf asdf 
4
128102389400760775

5.关系运算符==、!=、<=、>=、>、<

#include <string>
#include <iostream>
#include <set>
#include <map>
using namespace std;
int main()
{
string str;
map<string,int> words_cnt,m2;
if(words_cnt.empty())
	cout << "words_cnt is empty!!!" << endl;
while(cin >> str)
{
  words_cnt[str] ++;
}
cout << words_cnt.size() << endl;
cout << words_cnt.max_size() << endl;

cout << (words_cnt > m2) << endl;
if(words_cnt == m2)
	cout << "equal" << endl;
else 
 	cout << "not equal" << endl;
return 0;
}

运行如下:

words_cnt is empty!!!
asdf 
asdf asdf a
2
128102389400760775
1
not equal

6.迭代器c.cbegin()和c.cend()

#include <string>
#include <iostream>
#include <set>
#include <map>
using namespace std;
int main()
{
string str;
map<string,int> words_cnt,m2;
if(words_cnt.empty())
	cout << "words_cnt is empty!!!" << endl;
while(cin >> str)
{
  words_cnt[str] ++;
}
cout << words_cnt.size() << endl;
cout << words_cnt.max_size() << endl;

cout << (words_cnt > m2) << endl;
if(words_cnt == m2)
	cout << "equal" << endl;
else 
 	cout << "not equal" << endl;

cout << "print :" << endl;
map<string,int>::const_iterator it;
for(it = words_cnt.cbegin(); it != words_cnt.end() ; ++ it)
  {
     cout << it->first << endl;
     cout << it->second << endl;
  }
	
	
	
    return 0;
}

猜你喜欢

转载自blog.csdn.net/digitalkee/article/details/112967093
今日推荐