C++: How to Iterate the Elements over the Sets (set, unordered_s

  • 时间:2020-09-11 08:17:29
  • 分类:网络文摘
  • 阅读:136 次

In C++, we use set, multiset, unordered_multiset, unordered_set to store a hash set. C++ set/multiset implements a Red-Black tree that maintains the order of the elements. On the other hand, the unordered_set and unordered_multiset are based on Hashmap/Hashtable thus the elements are not sorted.

The multiset and unordered_multiset allows storing the duplicates in the set. To iterate over the sets, we can just use the simple for loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <unordered_set>
 
using namespace std;
 
int main() {
    unordered_set<int> data;
    data.insert(5);
    data.insert(3);
    data.insert(2);
    data.insert(3);
    data.insert(6);
    data.insert(7);
    for (const auto &n: data) {
        cout << n << endl;
    }
    return 0;
}
#include <iostream>
#include <unordered_set>

using namespace std;

int main() {
	unordered_set<int> data;
	data.insert(5);
	data.insert(3);
	data.insert(2);
	data.insert(3);
	data.insert(6);
	data.insert(7);
	for (const auto &n: data) {
		cout << n << endl;
	}
	return 0;
}

Duplicates are stored only once. And the unordered_set does not sort the elements. Thus you may see different order e.g. 7 6 2 5 3.

Iterating over the C++ set is similar. The output shows that elements are ordered. The C++ set O(logN) update/insert/delete/query is slower than unordered_set which has O(1) constant complexity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <set>
 
using namespace std;
 
int main() {
    set<int> data;
    data.insert(5);
    data.insert(3);
    data.insert(2);
    data.insert(3);
    data.insert(6);
    data.insert(7);
    for (const auto &n: data) {
        cout << n << endl; // prints 2 3 5 6 7
    }
    return 0;
}
#include <iostream>
#include <set>

using namespace std;

int main() {
	set<int> data;
	data.insert(5);
	data.insert(3);
	data.insert(2);
	data.insert(3);
	data.insert(6);
	data.insert(7);
	for (const auto &n: data) {
		cout << n << endl; // prints 2 3 5 6 7
	}
	return 0;
}

For multiset and unordered_multiset, iterating over the elements are similar except that the results will print the duplicates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <set>
 
using namespace std;
 
int main() {
    multiset<int> data;
    data.insert(5);
    data.insert(3);
    data.insert(2);
    data.insert(3);
    data.insert(6);
    data.insert(7);
    for (const auto &n: data) {
        cout << n << endl; // prints 2 3 3 5 6 7
    }
    return 0;
}
#include <iostream>
#include <set>

using namespace std;

int main() {
	multiset<int> data;
	data.insert(5);
	data.insert(3);
	data.insert(2);
	data.insert(3);
	data.insert(6);
	data.insert(7);
	for (const auto &n: data) {
		cout << n << endl; // prints 2 3 3 5 6 7
	}
	return 0;
}

Note that the C++ multiset is included in set header and unordered_multiset is provided in unordered_set header.

Alternatively, we can increment the iterator to start from begin() to end() of the sets: (set, multiset, unordered_set, and unordered_multiset). We use *it to de-reference the iterator pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <set>
 
using namespace std;
 
int main() {
    multiset<int> data;
    data.insert(5);
    data.insert(3);
    data.insert(2);
    data.insert(3);
    data.insert(6);
    data.insert(7);
    for (auto it = begin(data); it != end(data); it ++) {
        cout << *it << endl; // prints 2 3 3 5 6 7
    }
    return 0;
}
#include <iostream>
#include <set>

using namespace std;

int main() {
	multiset<int> data;
	data.insert(5);
	data.insert(3);
	data.insert(2);
	data.insert(3);
	data.insert(6);
	data.insert(7);
	for (auto it = begin(data); it != end(data); it ++) {
		cout << *it << endl; // prints 2 3 3 5 6 7
	}
	return 0;
}

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
惩罚性判罚或许可治食品安全问题之根  食物中的必需氨基酸和非必需氨基酸  食品营养价值及食品营养价值的评价  八大类食品对人体的营养价值分析  采用植物性原料种子做馅的月饼营养价值  几种含多不饱和脂肪酸的食物  维生素B2(核黄素)的食物来源  维生素B1(硫胺素)的食物来源  SAE数据库导入、导出和备份方法  保健食品:不以治疗疾病为目的! 
评论列表
添加评论