std::strings's capacity(), reserve() & resize() functions

后端 未结 6 1470
天命终不由人
天命终不由人 2020-11-27 17:27

I wan to use std::string simply to create a dynamic buffer and than iterate through it using an index. Is resize() the only function to actually allocate the buffer?

6条回答
  •  有刺的猬
    2020-11-27 18:16

    std::vector instead of std::string might also be a solution - if there are no requirements against it.

    vector v; // empty vector
    vector v(10); // vector with space for 10 elements, here char's
    

    Your example:

    vector my_string(20);
    
    int i=0;
    
    for ( parsing_something_else_loop )
    {
        char ch = ;
        my_string[i++] = ch;
    }
    

提交回复
热议问题