STL Learning Notes - Container array

Posted by Robkid on Tue, 14 May 2019 18:13:30 +0200

This article refers to "Houjie STL Video Tutorial" and the blog:
https://blog.csdn.net/mcyJacky/article/details/78570845
https://blog.csdn.net/sin_geek/article/details/51067874

1. array container

array is defined in the header file

namespace std
{
    template<class T, size_t N> 
    class array;
}

Array is a container with a fixed number of elements and has the same semantics as the corresponding C array. The size of the container array < T, N > equals the size of the corresponding C array T[N], and the same performance as the C array.However, it provides the appropriate features for C++ standard containers, such as querying container size, supporting replication, supporting random iterators, and so on.Setting the allocator is not supported.

There is a special case for std:: array < T, 0 > with a length of 0.At this point array.begin() == array.end(), and is equal to a specific value.The behavior of calling front() or back() on such arrays is undefined.

array is a simple collection (no constructors, no private or protected members) and can also be treated as a tuple with N elements of the same type.

Give an example:
array<int,10> iArray={1,2,3,4,5,6,7,8,9,10}; 

2. Code Testing

The code was compiled and tested in Dev C++ and the compiler supports the C++11 feature.

  • Both the.data() and.begin() methods take the first address of the container.
  • Declare an array container array <long, ASIZE> c, where long-type data is stored and the container length is ASIZE = 25000;
  • Declare a time variable timeStart, which calculates the time-consuming clock() - timeStart, when heading a file, by the time difference between before and after the program is run;
  • long-type values are generated using the random number function rand() and placed in containers during traversal.Note that time seeds are required before Rand () in order for the program to produce different random numbers each time;
  • The program uses the c language sorting method qsort() and search bsearch(), which must be sorted before searching.
  • The value found is dereferenced to *pItem.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <array>
#include <ctime>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::array;
using std::exception;
const long ASIZE = 250000L;

long get_a_target_long()
{
    long target = 0;
    cout<< "target (0~"<<RAND_MAX<<"):";
    cin>>target;

    return target;
} 

string get_a_target_string()
{
    long target = 0;
    char buf[10];

    cout<< "target (0~"<<RAND_MAX<<"):";
    cin>>target;
    snprintf(buf,10,"%d",target);
    return string(buf);
} 

int compareLongs(const void* a,const void* b)
{
    return ( *(long*)a - *(long*)b );
}

int compareStrings(const void* a,const void* b)
{
    if ( *(string*)a > *(string*)b )
        return 1;
    else if ( *(string*)a < *(string*)b )
        return -1;
    else
        return 0;
}


int main()
{
    cout << "test_array()......................\n";
    array<long, ASIZE> c;
    clock_t timeStart = clock();
    srand((unsigned)time(NULL)); //Seed, Generate Random
    for (long i = 0; i < ASIZE; ++i)
    {
        try
        {
            c[i] = rand();
        }
        catch (exception* p)
        {
            cout << "i = " << i << " " << p->what() << endl;
            abort();
        }
    }
    cout << "front: " << c[0] << endl;  //Numeric values in the first array container
    cout << "milli-seconds: " << (clock() - timeStart) << endl;  //Time required to store 25,000 numbers
    cout << "array.size() = " << c.size() << endl;//Size of array
    cout << "array.front() =  " << c.front() << endl; //Numeric values in the first array container
    cout << "array.back() = " << c.back() << endl;  //Values in the last array container
    cout << "array.data() = " << c.data() << endl; //Address of array container
    cout << "array.begin() = " << c.begin() << endl; //Address of array container
    long target = get_a_target_long();
    timeStart = clock();
    ::qsort(c.data(), ASIZE, sizeof(long), compareLongs);  //c Sort Method
    long* pItem = (long*)::bsearch(&target, c.data(), ASIZE, sizeof(long), compareLongs); //c Find Method
    cout << "qsort() + bsearch(), milli-seconds: " << (clock() - timeStart) << endl;
    if (pItem != NULL)
        cout << "found, " << *pItem << endl;
    else
        cout << "not found!" << endl;
    return 0;

}


Code output:

test_array()......................
front: 32451
milli-seconds: 19
array.size() = 250000
array.front() =  32451
array.back() = 779
array.data() = 0066B680
target (0~32767):2
qsort() + bsearch(), milli-seconds: 124
found, 2

3. Problems encountered

1) error C3861:'snprintf': No identifier found in VS2013

The snprintf() function is similar in format to printf and is used in c and is contained in the #include <stdio.h> header file.
However, the snprintf() function is not a function specified in standard c/c++, so in many compilers, vendors provide versions of their implementations.In gcc, the function name is snprintf(), whereas in VS it is called _snprintf.So if you need to use snprintf (), change to _snprintf

2) error C4996: '_snprintf': This function or variable may be unsafe. Consider using _snprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

This is not a syntax error, but IDE defaults to disallow this vulnerable old function, either by replacing it with a new, safer function or by adding the following line to the precompiled output:

#pragma warning(disable:4996)

Topics: C