logo

Zainicjuj wektor w C++

Wektor może przechowywać wiele wartości danych, takich jak tablice, ale może przechowywać tylko odniesienia do obiektów, a nie prymitywne typy danych. Przechowują referencje do obiektu, co oznacza, że ​​wskazują obiekty zawierające dane, zamiast je przechowywać. W przeciwieństwie do tablicy, wektory nie muszą być inicjowane rozmiarem. Mają możliwość elastycznego dostosowania w zależności od liczby odniesień do obiektów, co jest możliwe, ponieważ ich przechowywanie jest obsługiwane automatycznie przez kontener. Kontener będzie przechowywać wewnętrzną kopię alloc, która służy do alokacji miejsca na całe życie. Wektory można lokalizować i przeglądać za pomocą iteratorów, dzięki czemu są one umieszczane w ciągłym magazynie. Vector ma również funkcje bezpieczeństwa, które w przeciwieństwie do Array chronią programy przed awarią. Możemy zapewnić rezerwę miejsca wektorowi, ale nie tablicom. Tablica nie jest klasą, ale wektor jest klasą. W wektorze elementy można usuwać, ale nie w tablicach.

W przypadku nadrzędnej „klasy kolekcji” wektor jest wysyłany w postaci klasy szablonu. Tablica to struktura danych niższego poziomu z jej specyficznymi właściwościami. Wektory mają funkcje i konstruktory; nie są one oparte na indeksach. Są przeciwieństwem tablic, które są strukturami danych opartymi na indeksach. Tutaj najniższy adres jest podawany do pierwszego elementu, a najwyższy adres do ostatniego elementu. Vector służy do wstawiania i usuwania obiektów, natomiast Arrays służy do częstego dostępu do obiektów. Tablice to struktury danych oszczędzające pamięć, podczas gdy Vector wykorzystuje znacznie więcej pamięci w zamian za zarządzanie pamięcią masową i dynamiczny rozwój. Dostęp do elementów w wektorze zajmuje więcej czasu, ale w przypadku tablic nie jest to prawdą.

Istnieją cztery sposoby inicjowania a wektor w C++ :

  • Wprowadzając wartości pojedynczo
  • Używając przeciążonego konstruktora klasy wektora
  • Za pomocą tablic
  • Używając innego zainicjowanego wektora

Wpisując wartości pojedynczo -

Wszystkie elementy wektora można wstawiać jeden po drugim, korzystając z metody klasy wektorów „push_back”.

Algorytm

 Begin Declare v of vector type. Then we call push_back() function. This is done to insert values into vector v. Then we print 'Vector elements: 
'. ' for (int a: v) print all the elements of variable a.' 

Kod -

 #include #include using namespace std; int main() { vector vec; vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); vec.push_back(5); vec.push_back(6); vec.push_back(7); vec.push_back(8); vec.push_back(9); vec.push_back(101); for (int i = 0; i <vec.size(); i++) { cout << vec[i] ' '; } return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/62/initialize-vector-c.webp" alt="Initialize Vector in C++"> <h3>Using an overloaded constructor -</h3> <p>When a vector has multiple elements with the same values, then we use this method.</p> <p>By using an overloaded constructor of the vector class -</p> <p>This method is mainly used when a vector is filled with multiple elements with the same value.</p> <p> <strong>Algorithm</strong> </p> <pre> Begin First, we initialize a variable say &apos;s&apos;. Then we have to create a vector say &apos;v&apos; with size&apos;s&apos;. Then we initialize vector v1. Then initialize v2 by v1. Then we print the elements. End. </pre> <p> <strong>Code -</strong> </p> <pre> #include #include using namespace std; int main() { int elements = 12; vector vec(elements, 8); for (int i = 0; i <vec.size(); i++) { cout << vec[i] ' 
'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 8 8 8 8 8 8 8 8 8 8 8 8 </pre> <h3>By the help of arrays -</h3> <p>We pass an array to the constructor of the vector class. The Array contains the elements which will fill the vector.</p> <p> <strong>Algorithm -</strong> </p> <pre> Begin First, we create a vector say v. Then, we initialize the vector. In the end, print the elements. End. </pre> <p> <strong>Code -</strong> </p> <pre> #include #include using namespace std; int main() { vector vectr{9,8,7,6,5,4,3,2,1,0}; for (int i = 0; i <vectr.size(); i++) { cout << vectr[i] ' 
'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 9 8 7 6 5 4 3 2 1 0 </pre> <h3>Using another initialized vector -</h3> <p>Here, we have to pass the begin() and end() iterators of an initialized vector to a vector class constructor. Then we initialize a new vector and fill it with the old vector.</p> <p> <strong>Algorithm -</strong> </p> <pre> Begin First, we have to create a vector v1. Then, we have to initialize vector v1 by an array. Then we initialize vector v2 by v1. We have to print the elements. End. </pre> <p> <strong>Code -</strong> </p> <pre> #include #include using namespace std; int main() { vector vec_1{1,2,3,4,5,6,7,8}; vector vec_2(vec_1.begin(), vec_1.end()); for (int i = 0; i <vec_2.size(); i++) { cout << vec_2[i] ' 
'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 5 6 7 8 </pre> <hr></vec_2.size();></pre></vectr.size();></pre></vec.size();></pre></vec.size();>

Kod -

 #include #include using namespace std; int main() { int elements = 12; vector vec(elements, 8); for (int i = 0; i <vec.size(); i++) { cout << vec[i] \' 
\'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 8 8 8 8 8 8 8 8 8 8 8 8 </pre> <h3>By the help of arrays -</h3> <p>We pass an array to the constructor of the vector class. The Array contains the elements which will fill the vector.</p> <p> <strong>Algorithm -</strong> </p> <pre> Begin First, we create a vector say v. Then, we initialize the vector. In the end, print the elements. End. </pre> <p> <strong>Code -</strong> </p> <pre> #include #include using namespace std; int main() { vector vectr{9,8,7,6,5,4,3,2,1,0}; for (int i = 0; i <vectr.size(); i++) { cout << vectr[i] \' 
\'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 9 8 7 6 5 4 3 2 1 0 </pre> <h3>Using another initialized vector -</h3> <p>Here, we have to pass the begin() and end() iterators of an initialized vector to a vector class constructor. Then we initialize a new vector and fill it with the old vector.</p> <p> <strong>Algorithm -</strong> </p> <pre> Begin First, we have to create a vector v1. Then, we have to initialize vector v1 by an array. Then we initialize vector v2 by v1. We have to print the elements. End. </pre> <p> <strong>Code -</strong> </p> <pre> #include #include using namespace std; int main() { vector vec_1{1,2,3,4,5,6,7,8}; vector vec_2(vec_1.begin(), vec_1.end()); for (int i = 0; i <vec_2.size(); i++) { cout << vec_2[i] \' 
\'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 5 6 7 8 </pre> <hr></vec_2.size();></pre></vectr.size();></pre></vec.size();>

Za pomocą tablic -

Przekazujemy tablicę konstruktorowi klasy wektora. Array zawiera elementy, które wypełnią wektor.

Algorytm -

 Begin First, we create a vector say v. Then, we initialize the vector. In the end, print the elements. End. 

Kod -

 #include #include using namespace std; int main() { vector vectr{9,8,7,6,5,4,3,2,1,0}; for (int i = 0; i <vectr.size(); i++) { cout << vectr[i] \' 
\'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 9 8 7 6 5 4 3 2 1 0 </pre> <h3>Using another initialized vector -</h3> <p>Here, we have to pass the begin() and end() iterators of an initialized vector to a vector class constructor. Then we initialize a new vector and fill it with the old vector.</p> <p> <strong>Algorithm -</strong> </p> <pre> Begin First, we have to create a vector v1. Then, we have to initialize vector v1 by an array. Then we initialize vector v2 by v1. We have to print the elements. End. </pre> <p> <strong>Code -</strong> </p> <pre> #include #include using namespace std; int main() { vector vec_1{1,2,3,4,5,6,7,8}; vector vec_2(vec_1.begin(), vec_1.end()); for (int i = 0; i <vec_2.size(); i++) { cout << vec_2[i] \' 
\'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 5 6 7 8 </pre> <hr></vec_2.size();></pre></vectr.size();>

Używając innego zainicjowanego wektora -

W tym przypadku musimy przekazać iteratory Begin() i end() zainicjalizowanego wektora do konstruktora klasy wektorów. Następnie inicjujemy nowy wektor i wypełniamy go starym wektorem.

Algorytm -

 Begin First, we have to create a vector v1. Then, we have to initialize vector v1 by an array. Then we initialize vector v2 by v1. We have to print the elements. End. 

Kod -

 #include #include using namespace std; int main() { vector vec_1{1,2,3,4,5,6,7,8}; vector vec_2(vec_1.begin(), vec_1.end()); for (int i = 0; i <vec_2.size(); i++) { cout << vec_2[i] \\' 
\\'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 5 6 7 8 </pre> <hr></vec_2.size();>