Detailed explanation of the container operations of c++ associative containers (and the operations supported by sequential containers), based on c++primer 5th Table 9.2 (continuous update)

Both associative containers and sequential containers are containers, so both support container operations. It is the operation shown in Table 9.2. The following uses map as an example to test whether the operations in the table satisfy the associative container.

1. Type alias:

#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 file is a file that stores data.

All runs passed.

 

In summary, the g++ compiler does not support the subtraction operator of associative container iterators. The other maps in Table 9.2 are supported. You can experiment with the set situation.

2. Constructor

#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;
}

Summary: For the constructor

C c; C c1(c2); C c(c1.begin(),c1.end()); C c{p1,p2,p3...}; all hold, that is, the initialization of the map list has not been learned . No experiment here.

3. Assignment and 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;
}

 About the swap example:

#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() and c.clear() are both satisfied

#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;
}
~    

The results are as follows: 

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

5. Relational operator ==,! =, <=, >=, >, <

#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;
}

Run as follows:

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

6. Iterators c.cbegin() and 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;
}

 

Guess you like

Origin blog.csdn.net/digitalkee/article/details/112967093