C++ Container Library:: std ::array-II














































C++ Container Library:: std ::array-II



 
In the previous article we have discuss about array.If you have not gone through it you see through this link array . In continuation of the previous article we will learn more about array in this article.                              

  Declaration of std::array

The declaration of std::array is as follows:

       std::array<datatype,array_size> array_name;

  This is a simple declaration of an std::array. Let's look at the declaration of an integer array of length 6.

       std::array<int, 6> m;

   Here 'm' is an std::array of type int and length 6.

Initialization of std::array


Like arrays, we initialize an std::array by simply assigning it values at the time of declaration. For example, we will initialize an integer type std::array named 'm' of length 6 as shown below;
 
    std::array<int, 6> m = {1, 2, 3, 4, 5, 6};

There is another way of initializing an std::array which is shown below.

std::array<int, 6> m { {1, 2, 3, 4, 5, 6} }

 Example of Initialization of array:-

Code:-

 #include <iostream>

  #include <array>

  int main()

  {

   std::array<int, 6> m = { 1, 2, 3, 4, 5 , 6} ;

   std::cout << m[3] << std::endl;

   return 0;

}

Output:-

4

Length of std::array


We can return the length of an std::array using the size() function. The following example illustrates its use.


  #include <iostream>

  #include <array>

  int main()

  {

    std::array<int, 6> m = { 1, 2, 3, 4, 5, 6};

    std::cout << "Number of elements in the array are : " << m.size() << std::endl;

    return 0;

  }

Output:-

Number of elements in the array are : 6


Passing std::array to function


Let's look at an example of passing an std::array to a function.

Code:-

  #include <iostream>
  #include <array>
  using namespace std;

     void pArray(const std::array<int, 6> &m)
    {
      std::cout << "length of Array : " << m.size() << endl;
      for (long i = 0; i < m.size(); i++ )
        cout << "m[" << i << "] = " << m[i] << endl;
  
   }

  int main()
   {
    array<int, 6> m = {1,2,3,4,5,6};
    pArray(m);
    return 0;
  }

Output
 length of Array : 6
m[0] = 1
m[1] = 2
m[2] = 3
m[3] = 4
m[4] = 5
m[5] = 6




More Articles of SAYENDEEP MAHANTY:

Name Views Likes
C++ Exception Handling-|| 85 3
C++ Exception Handling 116 3
C++ File Handling EOF 94 5
C++ File Handling Error Handling During file Operation 1135 5
C++ File Handling seekp() Function 105 5
C++ File Handling seekg() Function 105 6
C++ File Handling tellg() Function 127 5
C++ File Handling tellp() Function 133 5
C++ File Handling close() Function 78 5
C++ File Handling:: open() 106 5
C++ File Handling Introduction 131 5
C++ Container Library:: std ::array-II 102 4
Top Django Interview Questions and Answers You Need to Know 302 6
C++ Container Library::std::array 96 6
C++ Container Library Introduction 103 5
Essential Engineering Skills for Your Resume 127 5
Mistakes To Avoid During Technical Interview 133 5
Mistakes You Must Avoid During Work From Home 178 6
Seven Common Mistakes that Beginners Should Avoid While Learning to Code 109 6
Simple Linear Regression 96 6
Python wagtailmenus 122 6
Python wagtail application 119 6
Python guppy GSL 136 5
Python wagtail guppy subpackage heapy 174 6
Python guppy Introduction 215 6
Python wagtail Structural Block 258 6
Python wagtail Integrating into Django Project 370 6
Python wagtail testing 281 7
Python wagtail Basic Block 349 7
Python wagtail image tag 216 7
Python wagtail Templates 179 6
Python wagtail Backends 206 6
Python wagtail Indexing 190 6
Python wagtail Search 173 7
Python wagtail Snippets 192 6
Python wagtail Writing your own page models 165 9
Python wagtail model class PageRevision 196 10
Python wagtail model class Site 199 9
Python wagtail wagtail core model 219 9
Python wagtail page models 189 10
Python wagtail 166 11

Comments