Using::std::vector<> as a preferred option for managing dynamic arrays

Source: Internet
Author: User
Tags arrays comparable manual

Absrtact: This article introduces the vector of container vectors in the C + + standard library, analyzes its advantages, and recommends that it be used as a priority choice for dynamic arrays in the application, rather than other class templates such as MFC carray<>. Finally, the vector interface and the precautions for use are introduced.

In some programs using MFC, often see a lot of programs using CARRAY<>, because of carray<> design problems, resulting in the complexity of the code to use it, increased maintenance difficulty. Therefore, it is recommended to use::std::vector<> instead of carray<>.

In addition, also see some programs in the use of malloc/realloc/free/new[]/delete[], such as manual management of memory. In an application, it is easy to manage memory manually and should use objects such as::std::vector<> to manage dynamic arrays.

Because of the less content in MSDN about:: std::vector, we do some introductions here for reference.

It doesn't matter if you're unfamiliar with carray<>/win32, there's not a lot of places to mention them.

1. carray<> VS::std::vector<>?

Carray<>, like::std::vector<>, are template classes that manage the dynamic array of objects of any type. Releases the managed dynamic memory as it is being refactored. Therefore, they can be used instead of manual dynamic array management.

But,carray<> was designed many years before C + + standardization (vc++2.0 times), at that time to the C + + programming, object-oriented programming, template design and other technical understanding of serious deficiencies, especially at that time to object-oriented technology false belief and propaganda, resulting in CArray There are major mistakes in the design of <>.

After the C + + language Standardization (1998), as well as VC + + 6.0 was born, provided the standard::std::vector<> template, basically in any respect is superior to carray<>. Microsoft has retained Carray<> because of its support for older programs, but it is clear that there is no intention of developing it according to new ideas (at least the operator= (CArray const&) should be provided.

To sum up,carray<> and::std::vector<> have the following differences:

1) carray<> is a,::std::vector<> in MFC that exists in any standard C + + implementation. Therefore, you use ripe carray<> can only be used in MFC, if used well:: Std::vector<>, you can use any platform in any C + + compiler. Using standard parts also helps others understand your program. Carray<> inherited CObject, just to achieve the serialization, which is inappropriate, violating the "you don t pay for what to you don ' t" of the C + + design principles. ::std::vector<> doesn't inherit anything, it just implements the management of a dynamic array.

2 carray<> is not an appropriate value type, for example, the following operations are illegal:

CArray<int,int> a;
CArray<int,int> b(a); // error, must use Copy().
b = a;    // error, must use Copy().
b == a;    // error, you must write your own.
b < a;    // error, you must write your own.

Contrary to the carray<>,::std::vector<> is a carefully designed value type that is inherently capable of copying constructs and assignable values. If T is comparable, then::std::vector<t> will automatically be comparable.

In addition, there are four special member functions involved;

T(); // 缺省构造函数(default constructor)
~T(); // 解构函数(destructor)
T( T const& ); // 拷贝构造函数
T& operator=( T const& ); // 拷贝赋值函数

Automatic generation, if you use CArray () as a member variable for T, the latter two of the four special functions mentioned above will not be automatically generated and need to be written manually:

struct T
{
  T() {}
  T( T const& t )
  {
    a_.Copy( t.a_ );
    i_ = t.i_;
    d_ = t.d_;
    s_ = t.s_;
  }
  T& operator = ( T const& t )
  {
    if( this != &t )
    {
      a_.Copy( t.a_ );
      i_ = t.i_;
      d_ = t.d_;
      s_ = t.s_;
    }
    return *this;
  }
private:
  CArray<int,int> a_;
  int i_;
  double d_;
  ::std::string s_;
};

If used:: Std::vector<>:

struct T
{
private:
  ::std::vector<int> a_;
  int i_;
  double d_;
  ::std::string s_;
};

The three special member functions listed above do not need to be written. The benefits are obvious: when you add or subtract a member variable of T, you do not have to go to the T (T const&) and operator= () to add or subtract accordingly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.