logo

Inicializujte Vector v C++

Vektor může ukládat více datových hodnot, jako jsou pole, ale mohou ukládat pouze odkazy na objekty a ne primitivní datové typy. Ukládají odkaz na objekt, což znamená, že ukazují na objekty, které obsahují data, místo aby je ukládaly. Na rozdíl od pole nemusí být vektory inicializovány velikostí. Mají flexibilitu pro přizpůsobení podle počtu referencí objektů, což je možné, protože jejich ukládání je řešeno automaticky kontejnerem. Kontejner bude uchovávat interní kopii alokace, která se používá k přidělení úložiště po celou dobu životnosti. Vektory lze lokalizovat a procházet pomocí iterátorů, takže jsou umístěny v souvislém úložišti. Vector má také bezpečnostní funkce, které na rozdíl od Array chrání programy před pádem. Můžeme dát rezervní prostor vektoru, ale ne poli. Pole není třída, ale vektor je třída. Ve vektoru lze prvky odstranit, ale ne v polích.

S nadřazenou 'třídou kolekce' je vektor odeslán ve formě třídy šablony. Pole je datová struktura nižší úrovně s jejich specifickými vlastnostmi. Vektory mají funkce a konstruktory; nejsou založeny na indexu. Jsou opakem Arrays, což jsou datové struktury založené na indexech. Zde je nejnižší adresa poskytnuta prvnímu prvku a nejvyšší adresa je poskytnuta poslednímu prvku. Vektor se používá pro vkládání a mazání objektu, zatímco pole se používá pro častý přístup k objektům. Pole jsou datové struktury šetřící paměť, zatímco Vector využívá mnohem více paměti pro správu úložiště a dynamický růst. Vektoru trvá přístup k prvkům déle, ale to není případ polí.

Existují čtyři způsoby inicializace a vektor v C++ :

  • Zadáním hodnot po jedné
  • Použitím přetíženého konstruktoru třídy vector
  • Pomocí polí
  • Pomocí jiného inicializovaného vektoru

Postupným zadáváním hodnot -

Všechny prvky ve vektoru lze vkládat jeden po druhém pomocí metody vektorové třídy 'push_back'.

Algoritmus

 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.' 

Kód -

 #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();>

Kód -

 #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();>

Pomocí polí -

Konstruktoru třídy vector předáme pole. Pole obsahuje prvky, které vyplní vektor.

Algoritmus -

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

Kód -

 #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();>

Použití jiného inicializovaného vektoru -

Zde musíme předat iterátory begin() a end() inicializovaného vektoru konstruktoru vektorové třídy. Poté inicializujeme nový vektor a naplníme jej starým vektorem.

Algoritmus -

 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. 

Kód -

 #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();>