2

Possible Duplicate:
C++ STL vector vs array in the real world

To start of I have a little below average knowledge of C++ and advanced knowledge of C
Many times, I want to ask something code related and include or refer to some code and an array comes up everyone instantly lock on this aspect and suggest I should instead use a vector, even if it doesn't have to do anything related to my problem.

The problem is that this is the way I learned how to use C++, obviously something I inherited from learning plain C first, and I am quite used to it, although I feel confident about using vectors also, I just prefer arrays over vectors.

My questions are why should someone use a vector instead of an array, what does he earn by doing so, even if he is quite used to arrays?
Also if arrays are not preferred over vectors why doesn't everyone simply use vectors for everything?

4

4 に答える 4

5

Vectors have a dimension that may be provided at run-time, such as a user-provided value or something derived from a file's contents.

The size of an array, by contrast, must be hard-coded into your program. It is fixed.

why doesn't everyone simply use vectors for everything?

There is a run-time performance cost for using vectors over arrays when you don't need to. Personally I've never written code in which this is a bottleneck and therefore rarely use raw arrays; still, such scenarios do exist.


The exception is when you create a block of memory (an "array" in some sense) dynamically with new[]; this is the exact language feature that is encapsulated by std::vector.

于 2013-01-11T00:12:11.830 に答える
0

One of the reasons is the performance behavior of each container, as explained here:

Many containers have several member functions in common, and share functionalities. The decision of which type of container to use for a specific need does not generally depend only on the functionality offered by the container, but also on the efficiency of some of its members (complexity). This is especially true for sequence containers, which offer different trade-offs in complexity between inserting/removing elements and accessing them.

于 2013-01-11T00:12:29.280 に答える
0

Vectors

  • do their own allocation and deallocation of memory
  • resize automatically when expanding
  • can be used where STL containers can, which includes many many algorithms and methods
  • have many other methods that are useful

Arrays

  • take less memory overall
  • Are used in C code, so are used for reverse compatibility
于 2013-01-11T00:12:38.607 に答える
0

主な理由は、 のサイズはstd::vector実行時に決定でき、サイズ変更可能であるためです。std::for_eachベクトルは反復子もサポートします。つまり、またはなどの多くの stl アルゴリズムでうまく機能しますstd::anyof。ベクトルは、C 関数と互換性のある安全な方法でメモリを割り当てるためにも使用できます。

Windows API を使用している場合std::vector、多くの関数で使用される get-size-get-data パラダイムで非常に役立ちます。これは通常、同じ関数を 2 回呼び出す場所です。1 回目はデータ サイズを取得するため、2 回目は実際にデータを読み取るためです。例えば:

DWORD size = 0;
DWORD type = 0;
if ( RegQueryValueEx( HKEY_LOCAL_MACHINE, 
    L"SOFTWARE\\Microsoft\\.NETFramework\\InstallRoot", 
    nullptr, 
    &type, 
    nullptr, 
    &size ) == ERROR_SUCCESS )
{
    std::vector<BYTE> data( size );
    if ( RegQueryValueEx( HKEY_LOCAL_MACHINE, 
        L"SOFTWARE\\Microsoft\\.NETFramework\\InstallRoot", 
        nullptr, 
        &type, 
        &data[0], 
        &size ) == ERROR_SUCCESS )
    {
      //We have read the data
    }
}

配列のサイズはコンパイル時に決定され、サイズを変更することはできません。C では配列がまだ使用されています。

于 2013-01-11T00:14:49.327 に答える