在Vector容器中有以下几个关于大小的函数
方法 | 效果 |
size() | 返回容器的大小 |
empty() | 判断容器是否为空 |
max_size() | 返回容器最大的可以存储的元素 |
capacity() | 返回容器当前能够容纳的元素数量 |
#include <vector> #include <string> #include <iostream> #include <algorithm> #include <iterator> using namespace std; int main() { vector<string> sentence; sentence.reserve(5); //append some elements sentence.push_back("hello"); sentence.push_back("how"); sentence.push_back("are"); sentence.push_back("you"); sentence.push_back("?"); copy(sentence.begin(), sentence.end(), ostream_iterator<string>(cout, " ")); cout << endl; //print "technical data" cout << "max_size():" << sentence.max_size() << endl; cout << "size():" << sentence.size() << endl; cout << "capacity():" << sentence.capacity() << endl; cout << "**************************" << endl; //swap second and fourth element swap(sentence[1], sentence[3]); //insert element "always" before element "?" sentence.insert(find(sentence.begin(), sentence.end(), "?"), "always"); //assign "!" to the last element sentence.back() = "!"; copy(sentence.begin(), sentence.end(), ostream_iterator<string>(cout, " ")); cout << endl; //print "technical data" // return maximum possible length of sequence //回容器的最大可以存储的元素个数,这是个极限,当容器扩展到这个最大值时就不能再自动增大 cout << "max_size():" << sentence.max_size() << endl; // return length of sequence cout << "size():" << sentence.size() << endl; // return current length of allocated storage cout << "capacity():" << sentence.capacity() << endl; system("Pause"); return 0; }