短い答え:使用リサイズ(10)の代わりに、予備(10)
長い答え:のstd ::文字列の実装で は、二つの変数のサイズと容量があります。 容量は、文字列に割り当てられたメモリの量です。 サイズは、あなたの文字列にいくつの有効な要素(あなたの場合はchar)が許されているかです。 容量は常にサイズ以下であることに注意してください。 reserve()を呼び出すと、容量が変更されます。
if (size > capacity){
capacity = max(size, capacity*2); //Why multiply capacity by 2 here? This is to achieve amortized O(1) while resizing
}
をここでOPが望んでいるのコード例を示します。この式は、その後適用されることになるで通話リサイズ()、あなたのサイズを変更する可能性があるだけでなく、サイズ>容量ならば、あなたも容量を変更します 、サイズや容量のより良い説明のためにいくつかのより多くのコード
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char const *argv[])
{ const char *s1 = "hello";
string s2;
s2 = s1;
cout << "length of s2 before reserve: " << s2.length() << endl;
cout << "capacity of s2 before reserve: " << s2.capacity() << endl;
s2.reserve(10);
cout << "length of s2 after reserve: " << s2.length() << endl; //see how length of s2 didn't change?
cout << "capacity of s2 after reserve: " << s2.capacity() << endl;
s2.resize(8); //resize(10) works too, but it seems like OP you only need enough size for 8 elements
cout << "length of s2 after resize: " << s2.length() << endl; //size changed
cout << "capacity of s2 after resize: " << s2.capacity() << endl; //capacity didn't change because size <= capacity
s2[5] = '.';
s2[6] = 'o';
s2[7] = '\0';
cout << "[" << s1 << "] [" << s2 << "]" << endl;
// You're done
// The code below is for showing you how size and capacity works.
s2.append("hii"); // calls s2.resize(11), s[8] = 'h', s[9] = 'i', s[10] = 'i', size = 8 + 3 = 11
cout << "length of s2 after appending: " << s2.length() << endl; // size = 11
cout << "capacity of s2 after appending: " << s2.capacity() << endl; //since size > capacity, but <= 2*capacity, capacity = 2*capacity
cout << "After appending: [" << s1 << "] [" << s2 << "]" << endl;
return 0;
結果:それは未定義
length of s2 before reserve: 5
capacity of s2 before reserve: 5
length of s2 after reserve: 5
capacity of s2 after reserve: 10
length of s2 after resize: 8
capacity of s2 after resize: 10
[hello] [hello.o]
length of s2 after appending: 11
capacity of s2 after appending: 20
After appending: [hello] [hello.ohii]
ですか?私は、プログラムが実行時にエラーになると思います。 – DDoSolitary
@Stalkerそうかもしれないし、そうでないかもしれない。 –
@Stalker:はい、未定義の動作です。範囲外のアクセス(例外)を定義したい場合は、 'at'を使用してください。 –