User Tools

Site Tools


cpp:stl:containers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
cpp:stl:containers [2020/05/28 17:10] – [Αναζήτηση στοιχείου] gthanoscpp:stl:containers [2020/05/28 17:44] – [Αναζήτηση στοιχείου] gthanos
Line 233: Line 233:
  
 === Αναζήτηση σε unordered set === === Αναζήτηση σε unordered set ===
 +
 +// unordered_set::find
 +#include <iostream>
 +#include <string>
 +#include <unordered_set>
 +
 +template<typename T>
 +void print_set(std::unordered_set<T>& myset) {
 +  std::cout << "myset contains:";
 +  for (auto it=myset.begin(); it!=myset.end(); ++it)
 +    std::cout << ' ' << *it;
 +  std::cout << '\n';
 +}
 +
 +int main () {
 +  std::unordered_set<std::string> myset;
 +  std::unordered_set<std::string>::iterator it;
 +  char* word[] = { "sugar", "choco", "milk", "banana", "coffee" };
 +
 +  // unordered_set some initial values:
 +  for (int i=0; i<5; i++) 
 +    myset.emplace(word[i]); 
 +  
 +  print_set(myset);
 +  
 +  it=myset.find(std::string("biscuits"));
 +  if(it != myset.end()) {
 +    myset.erase (it);
 +    std::cout << "'buiscuits' erased\n";
 +  }
 +  else 
 +    std::cout << "'buiscuits' not found\n";
 +  
 +  print_set(myset);
 +  myset.erase (myset.find(std::string("milk")));
 +  std::cout << "'milk' erased\n";
 +  print_set(myset);
 +
 +  return 0;
 +}
 +
 +<WRAP important 80% center round>
 +</WRAP>
    
cpp/stl/containers.txt · Last modified: 2022/05/26 16:49 by gthanos