0% found this document useful (0 votes)
35 views2 pages

Inserari in Vector

The document discusses various operations that can be performed on vectors in C++ including: 1) Inserting and removing elements from a vector using methods like push_back(), pop_back(), insert(), erase(), resize(), and reserve(). 2) Traversing a vector using iterators and reverse iterators. 3) Sorting the elements of a vector based on attributes of objects stored in the vector using a custom comparison function and sort(). 4) Removing elements from a vector that satisfy a given condition using remove_if().

Uploaded by

Denisa Dancu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Inserari in Vector

The document discusses various operations that can be performed on vectors in C++ including: 1) Inserting and removing elements from a vector using methods like push_back(), pop_back(), insert(), erase(), resize(), and reserve(). 2) Traversing a vector using iterators and reverse iterators. 3) Sorting the elements of a vector based on attributes of objects stored in the vector using a custom comparison function and sort(). 4) Removing elements from a vector that satisfy a given condition using remove_if().

Uploaded by

Denisa Dancu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Inserari in vector: struct Student

{ Student s;
v.push_back(element); //adaugare element la sfarsit char nume[10];
int n1; vector <Student> v;
v.insert(iterator, element); //adaugare element dupa iterator int n2;
double media;
v.swap(vector<t>); //interschimbare cu un alt vector; };

Stergeri in vector: Introducerea in vectorul v a elem din student:

v.pop_back(); //sterge ultimul element for (int i = 0; i < n; i++)


{
v.erase(iterator);//sterge elemental indicat de iterator f >> s.nume;
f >> s.n1;
v.erase(iterator, iterator);//sterge elementele din interval f >> s.n2;
s.media = double(s.n2 + s.n1) / 2.0;
v.resize(int, T);//schimba dimenisunea v.push_back(s);
}
v.reserve;//realocare memorie pt vector Afisarea:
v.empty();//test vector vid; void afisares(vector <Student> &v)
{
GENERIC void generare(vector <T> &v, int n) for (int i = 0; i < v.size(); i++)
{ {
for (int i = 0; i < n; i++) cout << "Studentul nr" << i+1 << endl;
v.push_back(rand() % 20);
} cout<<v[i].nume<<v[i].n1<< (etc);
cout << "Vectorul A dupa inserare, incepand cu pozitia 4 }
, 2 valori de 5: ";
A.insert(A.begin() + 3, 2, 5); cout<<endl;

cout << "Primul element din B: " << B.front() << endl; }

cout << "Ultimul element din B: " << B.back() << endl; Sortare dupa medie:

la eliminarea elementelor: i-- !!! bool med(Student s1, Student s2)


{
A.erase(A.begin() + i); if (s1.media != s2.media)
i--; return s1.media < s2.media;
else return strcmp(s1.nume, s2.nume) < 0;
cout << "Afisarea vectorului B folosind iteratori initial + inversat: "; }
vector <int> ::iterator it1;
for (it1 = B.begin(); it1 != B.end(); it1++) sort(v.begin(), v.end(), med);
cout << *it1 << ' ';
cout << endl; la eliminarea dupa o conditie:

vector<int> ::reverse_iterator it2; remove_if(v.begin(), v.end(), nota5); //nota 5 – bool


for (it2 = B.rbegin(); it2 != B.rend(); it2++)
v.erase(v.end() - 1); // !!!
cout << *it2;
cout<<endl;

You might also like