Tanto os contêineres associativos quanto os sequenciais são contêineres, portanto, ambos suportam operações de contêiner. É a operação mostrada na Tabela 9.2. O seguinte usa mapa como exemplo para testar se as operações na tabela satisfazem o contêiner associativo.
1. Digite o 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;
}
Arquivo arquivo é um arquivo que armazena dados.
Todas as corridas foram aprovadas.
Em resumo, o compilador g ++ não suporta o operador de subtração de iteradores de contêiner associativos. Os outros mapas na Tabela 9.2 são suportados. Você pode experimentar a situação definida.
2. Construtor
#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;
}
Resumo: Para o construtor
C c; C c1 (c2); C c (c1.begin (), c1.end ()); C c {p1, p2, p3 ...}; todos retêm, ou seja, a inicialização da lista de mapas não foi aprendido. Nenhum experimento aqui.
3. Atribuição e troca
#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;
}
Sobre o exemplo de troca:
#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. tamanho (), c. max_size (), c. empty () e c.clear () estão ambos satisfeitos
#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;
}
~
Os resultados são os seguintes:
words_cnt is empty!!!
asd fasd fasdf asdf asdf
4
128102389400760775
5. Operador relacional == ,! =, <=,> =,>, <
#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;
}
Execute da seguinte forma:
words_cnt is empty!!!
asdf
asdf asdf a
2
128102389400760775
1
not equal
6. Iteradores c.cbegin () e 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;
}