Difference between revisions of "CPP/STL/Set"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) (Created page with '==Set.cpp== <source lang="cpp"> #include <iostream> #include <set> #include <vector> #include <cstdlib> #include "Height.h" double RandomDouble(); void Report(std::set<double>…') |
Daviddoria (Talk | contribs) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 6: | Line 6: | ||
#include <cstdlib> | #include <cstdlib> | ||
| − | #include | + | #include <algorithm> |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
void TestSet(); | void TestSet(); | ||
| − | void | + | void TestSetIsInserted(); |
| − | + | ||
| − | + | ||
int main(int argc, char* argv[]) | int main(int argc, char* argv[]) | ||
{ | { | ||
//TestSet(); | //TestSet(); | ||
| − | + | TestSetIsInserted(); | |
| + | |||
| − | |||
return 0; | return 0; | ||
} | } | ||
| − | void | + | void TestSet() |
{ | { | ||
| − | // | + | // Create a set |
std::set<unsigned int> S; | std::set<unsigned int> S; | ||
| − | // | + | // Add 10 elements to the set |
| − | + | ||
| − | + | ||
| − | + | ||
for(unsigned int i = 0; i < 10; i++) | for(unsigned int i = 0; i < 10; i++) | ||
{ | { | ||
S.insert(i); | S.insert(i); | ||
} | } | ||
| − | + | ||
| − | // | + | // Output all of the elements in the set |
| − | + | for(std::set<unsigned int>::iterator it1 = S.begin(); it1 != S.end(); it1++) | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
{ | { | ||
| − | std::cout << " | + | std::cout << " " << *it1; |
} | } | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | std::cout << "Size: " << S.size() << " Empty? " << S.empty() << std::endl; | |
| − | + | ||
| − | + | ||
| − | std:: | + | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
} | } | ||
| − | + | void TestSetIsInserted() | |
| − | void | + | |
{ | { | ||
| − | std:: | + | // Create a set |
| + | std::set<unsigned int> S; | ||
| − | + | typedef std::pair<std::set<unsigned int>::iterator,bool> ReturnType; | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | // Add 10 elements to the set | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
for(unsigned int i = 0; i < 10; i++) | for(unsigned int i = 0; i < 10; i++) | ||
| − | + | { | |
| − | S.insert( | + | ReturnType inserted = S.insert(i); |
| − | + | if(inserted.second) | |
| − | + | { | |
| + | std::cout << "Inserted " << i << std::endl; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | std::cout << "Did not insert " << i << std::endl; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | ReturnType inserted = S.insert(3); | ||
| + | if(inserted.second) | ||
| + | { | ||
| + | std::cout << "Inserted " << 3 << std::endl; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | std::cout << "Did not insert " << 3 << std::endl; | ||
| + | } | ||
} | } | ||
| − | + | </source> | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ==CMakeLists.txt== | |
| − | + | <source lang="cmake"> | |
| − | + | cmake_minimum_required(VERSION 2.6) | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | Project(Set) | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ADD_EXECUTABLE(Set Set.cpp) | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
</source> | </source> | ||
Latest revision as of 16:15, 14 February 2011
Set.cpp
#include <iostream> #include <set> #include <vector> #include <cstdlib> #include <algorithm> void TestSet(); void TestSetIsInserted(); int main(int argc, char* argv[]) { //TestSet(); TestSetIsInserted(); return 0; } void TestSet() { // Create a set std::set<unsigned int> S; // Add 10 elements to the set for(unsigned int i = 0; i < 10; i++) { S.insert(i); } // Output all of the elements in the set for(std::set<unsigned int>::iterator it1 = S.begin(); it1 != S.end(); it1++) { std::cout << " " << *it1; } std::cout << "Size: " << S.size() << " Empty? " << S.empty() << std::endl; } void TestSetIsInserted() { // Create a set std::set<unsigned int> S; typedef std::pair<std::set<unsigned int>::iterator,bool> ReturnType; // Add 10 elements to the set for(unsigned int i = 0; i < 10; i++) { ReturnType inserted = S.insert(i); if(inserted.second) { std::cout << "Inserted " << i << std::endl; } else { std::cout << "Did not insert " << i << std::endl; } } ReturnType inserted = S.insert(3); if(inserted.second) { std::cout << "Inserted " << 3 << std::endl; } else { std::cout << "Did not insert " << 3 << std::endl; } }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) Project(Set) ADD_EXECUTABLE(Set Set.cpp)