अभी तक आपने को भी examples देखे , उनमे हमें variables को अलग - अलग define करना पड़ता था , लेकिन variables में एक problem थी कि वह सिर्फ single value को store कर सकता है।

Let's suppose की हमें integer type के 5 variables use करने हैं तो सभी define करने पड़ेंगे। जिससे code बड़ा हो जयगा और manage करना भी मुश्किल , इस problem को solve करने के लिए C++ में एक data structure provide किया : Array .

C++ में array same data types values का एक collection है। means हम एक single array variable में same data type की एक ज्यादा values को store कर सकते हैं।

c++ define array

C++ में array को उसके type और size के साथ brackets का use करके define किया जाता है। type बताता है कि , array किस type की values को store करेगा और size बताता है कि array में कुल कितने elements होंगे।

type array_name[size];

or

type array_name[size] = {comma separated elements according to size};

Note - C++ में array define करने के लिए हमें array का type और size define करने की जरूरत होती है। लेकिन कुछ languages जैसे JavaScript और PHP में array define करते समय array type और size define करने की कोई जरूरत नहीं होती है। और उसमे किसी भी type की values store करा सकते हैं।

c++ array example

string users[3] = {"Rahul", "Ravi", "Raju"};

चूंकि array में values एक से ज्यादा हैं , तो हर value की एक position होती है जिसे index कहते हैं। Array में value की indexing 0 से n-1 होती है। यह n कुल elements की सख्यां है। इन्ही index के bases पर array values को insert / update किया जाता है। For Example ऊपर दिए गए example में values की indexing कुछ इस तरह से होगी।

Index

0

1

2

Values

Rahul

Ravi

Raju

ध्यान रहे एक बार array define करने के बाद आप उसके size को change नहीं कर सकते हैं , और न ही दूसरे type की value insert कर सकते हैं।

c++ access array element

तो जैसे कि आपने अभी पढ़ा कि index के bases पर array values को insert / update किया जाता है तो ऊपर define किये गए array elements को कुछ इस तरह से access करेंगे।

cout << users[0] // Output : Rahul
cout << users[1] // Output : Ravi
cout << users[2] // Output : Raju

c++ update array values

इसी तरह index का use करके आप directly new value assign करके array value को update कर सकते हैं , जैसे -

users[0] = "New value";

#include <iostream> using namespace std; int main() { // define array. string users[3] = {"Rahul", "Ravi", "Raju"}; // accessing array elements; cout << users[0] << endl; cout << users[1] << endl; cout << users[2] << endl; // update array values. users[0] = "Shyam"; // print new value cout << users[0]; return 0; }

c++ advantages of array

  • Code Optimization (less code)

  • Random Access

  • Easy to traverse data

  • Easy to manage data

  • Easy to understand.

c++ disadvantage of array

c++ में Array का एक ही main disadvantage है , कि आप एक बार Array का size define करने के बाद उसे बाद में change नहीं कर सकते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers