2011-01-26 5 views
2

C++のint []配列を変数で定義された0から数にする必要がありますが、ISO C++では可変長配列を禁止します... 簡単にアレイ?私はメモリを確保/解放する必要がありますか?0から定義された番号までint配列を埋めてください

int possibilities[SIZE]; 
unsigned int i = 0; 
for (i = 0; i < SIZE; i++) { 
    possibilities[i] = i; 
} 

btw。もしあなたが尋ねるなら、私は正確に標準のint []配列、ベクトル、マップなどは不要です。

+1

なぜ 'std :: vector'を使うことができないのですか? – sharptooth

+0

私はnext_permutation()に配列を渡す必要があります...そして、私はそれが可能な限り簡単に必要なので、私はベクトルの別の "順列"関数を構築したくありません... –

+5

あなたが知っている標準的なC配列としてもベクターですか? int * array =&myvec [0]を使用するだけです。配列は標準配列として使用可能で、基本となるベクトルに要素を追加しない限り固定されます。 – jcoder

答えて

13

を人間の助けなければなりません。だからあなたの選択肢は、ヒープ上に割り当てるために(メモリ管理の問題を紹介)、またはその代わりにCスタイルの配列のstd::vectorを使用するか、次のとおりです。

std::vector<int> possibilities(SIZE); 
for (int i = 0; i < SIZE; i++) 
{ 
    possibilities[i] = i; 
} 

あなたはもっと派手な取得したい場合は、することができますあなたのためにこのシーケンスを生成するためのSTLを使用します。

// This is a "functor", a class object that acts like a function with state 
class IncrementingSequence 
{ 
public: 
    // Constructor, just set counter to 0 
    IncrementingSequence() : i_(0) {} 
    // Return an incrementing number 
    int operator()() { return i_++; } 
private: 
    int i_; 
} 

std::vector<int> possibilities(SIZE); 
// This calls IncrementingSequence::operator() for each element in the vector, 
// and assigns the result to the element 
std::generate(possibilities.begin(), possibilities.end(), IncrementingSequence); 
+0

'generate_n'は要件をさらに近づけます。 – xtofl

+0

@ニム:良いキャッチ! –

+0

@xtofl:この場合、大きな違いはないと思います。 OPは配列全体をベクトルで塗りつぶしたいだけです。私が理解しているように、 'std :: generate_n'は、全体を埋める必要がない場合に便利です。 –

0

SIZEを定数(マクロまたはconst)にすると、それを使って静的アレイ。定数を使うことができない場合は、プログラムの外から意図したサイズを読み込んでいるなど、メモリを割り当てる必要があります。

要するに、コンパイル時にサイズがわからない場合は、おそらく実行時にメモリを割り当てる必要があります。

-2

それはuはあなたが見つけてきたように、あなたがスタック上の可変長配列を作成することはできません

int* a = NULL; // Pointer to int, initialize to nothing. 
int n;   // Size needed for array 
cin >> n;  // Read in the size 
a = new int[n]; // Allocate n ints and save ptr in a. 
for (int i=0; i<n; i++) { 
    a[i] = 0; // Initialize all elements to zero. 
} 
. . . // Use a as a normal array 
delete [] a; // When done, free memory pointed to by a. 
a = NULL;  // Clear a to prevent using invalid memory reference 
+0

C++を初めて使う人には、 'new []'と 'delete []'はお勧めしません。 – rightfold

+0

@райтфолд:なぜか分かりますか?彼らはどこかから始めるべきです...私はBorland C++(DOS)のバージョンでプログラミングしていました... – Amir

+0

彼らは危険なツールなので使用しないでください?常に 'std :: vector'を使います。 – rightfold

2
std::vector<int> possibilities; 
unsigned int i = 0; 
for (i = 0; i < SIZE; i++) { 
    possibilities.push_back(i); 
} 

使用std::vectorあなたがpをしたい場合

(あなたが<vector>を含める必要があります)あなたが書く必要がstd::next_permutationにお尻ベクトル:

std::next_permutation(possibilities.begin(),possibilities.end()); 

また、あなたは、Cスタイルの配列としてベクトルを使用することができます。 &vec[0]はCスタイルの配列へのポインタを返します。

struct increment { 
int value; 
int operator()() { return ++value; } 
increment():value(0){} 
}; 
1

あなたがstd::generate_n機能を使用することができますか?

type * pointer; 
pointer = new type[number_of_elements]; 

void main() 
{ 
int limit = 0; // Your lucky number 
int * pointer = NULL; 
cout << "Please, enter limit number: ";   
cin >> n; 
pointer = new int[limit+1]; // Just to be sure. 
    for (int i = 0; i < n; i++) 
    { 
     pointer[i] = i; // Another way is: *(pointer+i) = i (correct me if I'm wrong) 
    } 
delete [] pointer; // Free some memory 
pointer = NULL; // If you are "pedant" 
} 

これは最善の解決策ではありません。私はそれが助けて欲しい

+0

オペレータはoperator()(){... –

+0

@BenJ:そうです。ありがとう。 – xtofl

-2

は、単に動的配列を使用します。incrementは数字を生成したオブジェクトがある

std::generate_n(myarray, SIZE, increment()); 

+1

C++を初めて使う人には 'new []'と 'delete []'をお薦めしません。 – rightfold

+0

申し訳ありませんが、私はそのstd :: vector 可能(SIZE);より複雑です。 – bpavlov

+0

@rightfold質問もご覧ください。 "私はまさに標準のint []配列、ベクトル、地図などは必要ありません" – bpavlov

2

ブーストにアクセスできない場合は、すでに増分イテレータにアクセスできます。

#include <vector> 
#include <boost/iterator/counting_iterator.hpp> 

std::vector<int> possibilities(
    boost::counting_iterator<int>(0), 
    boost::counting_iterator<int>(SIZE)); 

counting iteratorは基本的に値をインクリメントします。したがって、開始値と終了値を自動的に伝え、ベクトルが自動的に適切に設定されます。

他のところで述べたように、得られたベクターはstd :: next_permutationと直接使用できます。

std::next_permutation(possibilities.begin(),possibilities.end()); 
12

std :: iotaとstd :: arrayを使用できます。以下の例は、配列が天然にはstd ::イオタもベクターで動作する1から10までの値

std::array<int, 10> a; 
std::iota(a.begin(), a.end(), 1); 

編集 と10の大き埋めます。

関連する問題