User Tools

Site Tools


cpp:stl:containers

This is an old revision of the document!


Κοινές συναρτήσεις για όλους τους containers

Εισαγωγή στοιχείου

Με εξαίρεση την κλάση std::array που το μέγεθος των πινάκων που δημιουργεί είναι σταθερό και δηλώνεται κατά τη δήλωση του πίνακα, οι υπόλοιποι containers μπορούν να μεταβάλλουν το αριθμό των στοιχείων που αποθηκεύουν. Για την εισαγωγή ενός στοιχείου σε έναν container υπάρχουν οι συναρτήσεις insert για την εισαγωγή ενός αντιγράφου του στοιχείου στον container και emplace για την δημιουργία ενός αντικειμένου και εισαγωγή του στον container. Επιπλέον, μόνο για τους sequence containers η insert λαμβάνει ως πρώτο όρισμα έναν iterator που δηλώνει τη θέση εισαγωγής του στοιχείου στον container.

Παραδείγματα:

A. Ένθεση σε λίστα

student_list_insert.cpp
#include <iostream>     // std::cout
#include <algorithm>    // std::copy
#include <list>         // std::list
#include "Student.hpp"
 
int main () {
  Student students[] = { Student("Peter_Pan", 1234), Student("Tinker_Bell", 1235) };
 
  std::cerr << "----- Init list -----" << std::endl;
  std::list<Student> mylist;
  for(int i=0; i<2; i++) {
    mylist.insert(mylist.begin(),students[i]);  // copy-constructor, insert first
    mylist.insert(mylist.end(),students[i]);    // copy-constructor, insert last
  }
 
  mylist.emplace(mylist.end(), "Mickey_Mouse", 1237);  // argument construct, insert last
 
  std::cerr << "-------------------------\n";
  std::cerr << "mylist contains:";
  for (std::list<Student>::iterator it = mylist.begin(); it!=mylist.end(); ++it)
    std::cerr << ' ' << *it;
  std::cerr << std::endl;
  std::cerr << "-------------------------\n";
 
  return 0;
} 

Β. Ένθεση σε set

string_set_insert.cpp
#include <iostream>     // std::cout
#include <algorithm>    // std::copy
#include <set>       // std::set
#include <string>
 
int main () {
  std::string strings[] = { std::string("gamma"), std::string("beta"), std::string("delta") };
 
  std::cerr << "----- Init set -----" << std::endl;
  std::set<std::string> myset;
  for(int i=0; i<3; i++) 
    myset.insert(strings[i]);
 
  myset.emplace("alpha");  // argument construct, insert last
 
  std::cerr << "-------------------------\n";
  std::cerr << "myset contains:";
  for (std::set<std::string>::iterator it = myset.begin(); it!=myset.end(); it++)
    std::cerr << ' ' << *it;
  std::cerr << std::endl;
  std::cerr << "-------------------------\n";
 
  return 0;
} 

Διαγραφή στοιχείου

Η διαγραφή στοιχείου είναι ανάλογη της εισαγωγής και γίνεται μέσω της συνάρτησης erase. H συνάρτηση επιστρέφει έναν iterator στο επόμενο στοιχείο από αυτό που έχει διεγραφεί. Για τους sequence containers σε αναλογία με την διαδικασία εισαγωγής, η συνάρτηση λαμβάνει ως όρισμα έναν iterator που δηλώνει τη θέση διαγραφής από τον container. Για τους associative containers η διαγραφή γίνεται παρέχοντας ως όρισμα τη θέση του κλειδιού (μέσω iterator) ή την τιμή του κλειδιού που επιθυμούμε να διαγράψουμε.

Διαγραφή από λίστα

Το παρακάτω παράδειγμα είναι από τη σελίδα http://cplusplus.com/list/erase

list_erase.cpp
#include <iostream>
#include <list>
 
int main () {
  std::list<int> mylist;
  std::list<int>::iterator it1, it2;
 
  // insert values:
  for (int i=1; i<10; ++i) mylist.push_back(i*10);
 
                              // 10 20 30 40 50 60 70 80 90
  it1 = it2 = mylist.begin(); // ^^
  advance (it2,6);            // ^                 ^
  ++it1;                      //    ^              ^
 
  it1 = mylist.erase (it1);   // 10 30 40 50 60 70 80 90
                              //    ^           ^
 
  it2 = mylist.erase (it2);   // 10 30 40 50 60 80 90
                              //    ^           ^
                              // 10 30 40 50 60 80 90
  ++it1;                      //       ^        ^
  --it2;                      //       ^     ^
 
  it1=mylist.erase (it1,it2); // 10 30 60 80 90
                              //        ^
 
  std::cout << "mylist contains:";
  for (it1=mylist.begin(); it1!=mylist.end(); ++it1)
    std::cout << ' ' << *it1;
  std::cout << '\n';
 
  return 0;

Διαγραφή από set

Το παρακάτω παράδειγμα είναι από τη σελίδα http://cplusplus.com/set/erase

set_erase.cpp
// erasing from set
#include <iostream>
#include <set>
 
int main ()
{
  std::set<int> myset;
  std::set<int>::iterator it;
 
  // insert some values:
  for (int i=1; i<10; i++) myset.insert(i*10);  // 10 20 30 40 50 60 70 80 90
 
  it = myset.begin();
  ++it;                                         // "it" points now to 20
 
  myset.erase (it);
 
  myset.erase (40);
 
  it = myset.find (60);
  myset.erase (it, myset.end());
 
  std::cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';
 
  return 0;
}
cpp/stl/containers.1590683790.txt.gz · Last modified: 2020/05/28 15:36 (external edit)