How to shrink vector capacity to fit in C++

1 Answer

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

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

	vec_str.reserve(5);

	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;

	vec_str.shrink_to_fit();

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

	return 0;
}

/*
run:

size() =     2
capacity() = 5
size() =     2
capacity() = 2

*/

 



answered Jan 4, 2018 by avibootz

Related questions

...