User Tools

Site Tools


cpp:stl:container_common_functions

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
cpp:stl:container_common_functions [2022/06/01 04:23] gthanoscpp:stl:container_common_functions [2023/05/29 16:13] – [A. Ένθεση στους sequence_containers list, forward_list, vector και dequeue] gthanos
Line 39: Line 39:
  
 </code> </code>
 +
 +<WRAP tip 80% center round>
 +Η διαφορά ανάμεσα στις μεθόδους //insert// και //emplace// είναι η εξής: 
 +  * Στη μέθοδο //insert// παρέχουμε ένα αντικείμενο και η μέθοδος κατασκευάζει ένα αντίγραφο του μέσω του κατασκευαστή αντιγραφέα (copy-constructor) της κλάσης //Student//.
 +  * Στη μέθοδο //empace// παρέχουμε τα ορίσματα μέσω των οποίων κατασκευάζεται ένα αντικείμενο τύπου Student, μέσω του κατασκευαστή ''Student(const char *name, int aem);''.
 +</WRAP>
  
 ==== Β. Ένθεση σε set, unordered set ==== ==== Β. Ένθεση σε set, unordered set ====
Line 53: Line 59:
   Student students[] = { Student("Peter_Pan", 1234), Student("Tinker_Bell", 1235) };   Student students[] = { Student("Peter_Pan", 1234), Student("Tinker_Bell", 1235) };
                                                      
-  std::cerr << "----- Init list -----" << std::endl; +  std::cerr << "----- Init set -----" << std::endl; 
-  std::set<Student> mylist;+  std::set<Student> myset;
   for(int i=0; i<2; i++) {   for(int i=0; i<2; i++) {
-    mylist.insert(students[i]);  // copy-constructor, insert first +    myset.insert(students[i]);  // copy-constructor, insert first 
-    mylist.insert(students[i]);    // copy-constructor, insert last+    myset.insert(students[i]);    // copy-constructor, insert last
   }   }
      
-  mylist.emplace("Mickey_Mouse", 1237);  // argument construct, insert last+  myset.emplace("Mickey_Mouse", 1237);  // argument construct, insert last
      
   std::cerr << "-------------------------\n";   std::cerr << "-------------------------\n";
-  std::cerr << "mylist contains:"; +  std::cerr << "myset contains:"; 
-  for (std::set<Student>::iterator it = mylist.begin(); it!=mylist.end(); ++it)+  for (std::set<Student>::iterator it = myset.begin(); it!=myset.end(); ++it)
     std::cerr << ' ' << *it;     std::cerr << ' ' << *it;
   std::cerr << std::endl;   std::cerr << std::endl;
cpp/stl/container_common_functions.txt · Last modified: 2023/05/30 19:18 by gthanos