int main() {
    std::string s;
    for (int i = 0 ; i < 345; ++i) { s += '0'; }    
    cout << s.size() << " " << s.capacity() << endl; 
    string(s).swap(s);
    cout << s.size() << " " << s.capacity() << endl; 
    /* differ from vector, seems it do not work to release the extra space 
     * print  
     * 345 512
     * 345 512 
     */
    std::vector<int> vec;
    for (int i = 0 ; i < 345; ++i) {
        vec.push_back(i);
    }
    cout << vec.size() << " " << vec.capacity() << endl;
    vector<int>(vec).swap(vec);
    cout << vec.size() << " " << vec.capacity() << endl;
    /* 
     * print : extra free space is released 
     * 345 512
     * 345 345 
     */
    return 0;
}

shiyang6017
164 声望60 粉丝