0

その質問によると、私は自分のプロジェクトでのメモリの管理についてますます心配しており、配列メモリの 1 つに競合があると、プログラムがクラッシュします。

この投稿された質問に対するさまざまな回答は、配列をベクトルに変更し、配列の一部を以下のように定義するという事実に収束しました。

#define Nb 50
int position[Nb];
double indication[Nb];
double sum[Nb];

それぞれをどのように変更するか

 std::vector
 std::list?

宣言を変更するときにそれらを操作/処理できますか (たとえば、昇順で整理したり、値の1つを上書きしたりします)?

4

2 に答える 2

2

固定サイズの配列を使用している、

#define Nb 50
int position[Nb];

これらに代わる最新の C++ はstd::array、 ではなくstd::vectorです。

std::array<int, 50> position;

std::vectorサイズが動的な配列で、そのサイズは実行時に決定でき、構築後に変更できます。

int n;
std::cin >> n;
std::vector<int> v(n); // vector has n value constructed ints
v.push_back(42); // v has n+1 ints now.

C++14std::dynarrayは、動的にサイズ変更される配列である を提供します。この配列のサイズは実行時に決定できますが、構築後に変更することはできません。

于 2013-06-19T08:55:19.613 に答える
1

The Syntax is as follows:

#define Nb 50
std::vector<int> position(Nb); // create std::vector holding ints, Initial size is 50

You should also refrain from using #define, but rather use const, this is more C++:

const int Nb = 50; //maybe wrap Nb in a namespace
 std::vector<int> position(Nb);

See vector and its constructors.

于 2013-06-19T08:50:52.657 に答える