How to get vector size and capacity in C++

2 Answers

0 votes
#include <iostream>  
#include <string>  
#include <vector>  

int main()
{
	std::vector<std::string> vec_str;

	vec_str.reserve(4);

	std::cout << "size() =     " << vec_str.size() << std::endl;
	std::cout << "capacity() = " << vec_str.capacity() << std::endl;

	return 0;
}

/*
run:

size() =     0
capacity() = 4

*/

 



answered Jan 3, 2018 by avibootz
0 votes
#include <iostream>  
#include <string>  
#include <vector>  

int main()
{
	std::vector<std::string> vec_str;

	vec_str.reserve(4);

	vec_str.push_back("c++");
	vec_str.push_back("c");

	std::cout << "size() =     " << vec_str.size() << std::endl;
	std::cout << "capacity() = " << vec_str.capacity() << std::endl;

	return 0;
}

/*
run:

size() =     2
capacity() = 4

*/

 



answered Jan 3, 2018 by avibootz

Related questions

2 answers 197 views
1 answer 191 views
1 answer 125 views
1 answer 145 views
1 answer 205 views
1 answer 130 views
130 views asked Feb 7, 2023 by avibootz
...