xxxxxxxxxx
// vector::size
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myints;
std::cout << "0. size: " << myints.size() << '\n';
for (int i=0; i<10; i++) myints.push_back(i);
std::cout << "1. size: " << myints.size() << '\n';
myints.insert (myints.end(),10,100);
std::cout << "2. size: " << myints.size() << '\n';
myints.pop_back();
std::cout << "3. size: " << myints.size() << '\n';
return 0;
}
xxxxxxxxxx
#include <vector>
int main() {
std::vector<int> myVector = { 666, 1337, 420 };
size_t size = myVector.size(); // 3
myVector.push_back(399); // Add 399 to the end of the vector
size = myVector.size(); // 4
}
xxxxxxxxxx
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> a(8, 57);
a.push_back(371); // Adds 371 at the end
a.pop_back(); // Removes the last element
a.resize(14, -25); // Increases the size to 14, adding -25
a.resize(7, -25); // Reduces the size by 1 element
a.assign(10, 23); // Clears the previous vector and adds 10 elements with the value 23
bool f = a.empty(); // Checks if the vector is empty
a.clear(); // Makes the vector empty
int x = a.back(); // Returns the last element
a.back() = 2; // Sets the last element to 2
x = a.front(); // Reference to the first element of the vector
a.front() += 4; // Increases the first element by 4
return 0;
}
xxxxxxxxxx
size() function is used to return the size of the vector container or the number of elements in the vector container.
using namespace std;
int main(){
vector<int> myvector{ 1, 2, 3, 4, 5 };
cout << myvector.size();
return 0;
}
//Output = 5