Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C++

C++ में Array क्लास


C++ में Array क्लास पर्याप्त कुशल है और यह अपना आकार भी जानती है।

ऐरे पर ऑपरेशन करने के लिए उपयोग किए जाने वाले फंक्शन्स

  • size() = ऐरे का आकार लौटाने के लिए यानी ऐरे के एलिमेंट्स की संख्या लौटाता है।
  • max_size() = ऐरे के अधिकतम एलिमेंट्स की संख्या लौटाने के लिए।
  • get(), at(), operator[] = ऐरे एलिमेंट्स तक पहुंच प्राप्त करने के लिए।
  • front() = ऐरे के फ्रंट एलिमेंट को लौटाने के लिए।
  • back() = ऐरे के अंतिम एलिमेंट को लौटाने के लिए।
  • empty() = यदि ऐरे का आकार सही है तो true लौटाता है अन्यथा false।
  • fill() = पूरे ऐरे को एक विशेष मान से भरने के लिए।
  • swap() = एक ऐरे के एलिमेंट्स को दूसरे ऐरे से स्वैप करने के लिए।

यहां ऊपर बताए गए सभी ऑपरेशन्स को लागू करने का एक उदाहरण है −

उदाहरण कोड

#include<iostream> #include<array> using namespace std; int main() {    array<int,4>a = {10, 20, 30, 40};    array<int,4>a1 = {50, 60, 70, 90};    cout << "The size of array is : ";    //size of the array using size()    cout << a.size() << endl;    //maximum no of elements of the array    cout << "Maximum elements array can hold is : ";    cout << a.max_size() << endl;    // Printing array elements using at()    cout << "The array elements are (using at()) : ";    for ( int i=0; i<4; i++)       cout << a.at(i) << " ";       cout << endl;    // Printing array elements using get()    cout << "The array elements are (using get()) : ";    cout << get<0>(a) << " " << get<1>(a) << " "<<endl;    cout << "The array elements are (using operator[]) : ";    for ( int i=0; i<4; i++)       cout << a[i] << " ";       cout << endl;    // Printing first element of array    cout << "First element of array is : ";    cout << a.front() << endl;    // Printing last element of array    cout << "Last element of array is : ";    cout << a.back() << endl;    cout << "The second array elements before swapping are : ";    for (int i=0; i<4; i++)       cout << a1[i] << " ";       cout << endl;    // Swapping a1 values with a    a.swap(a1);    // Printing 1st and 2nd array after swapping    cout << "The first array elements after swapping are : ";    for (int i=0; i<4; i++)       cout << a[i] << " ";       cout << endl;       cout << "The second array elements after swapping are : ";    for (int i = 0; i<4; i++)       cout << a1[i] << " ";       cout << endl;    // Checking if it is empty    a1.empty()? cout << "Array is empty":    cout << "Array is not empty";    cout << endl;    // Filling array with 1    a.fill(1);    // Displaying array after filling    cout << "Array content after filling operation is : ";    for ( int i = 0; i<4; i++)       cout << a[i] << " ";       return 0; }

आउटपुट

The size of array is : 4 Maximum elements array can hold is : 4 The array elements are (using at()) : 10 20 30 40 The array elements are (using get()) : 10 20 The array elements are (using operator[]) : 10 20 30 40 First element of array is : 10 Last element of array is : 40 The second array elements before swapping are : 50 60 70 90 The first array elements after swapping are : 50 60 70 90 The second array elements after swapping are : 10 20 30 40 Array is not empty Array content after filling operation is : 1 1 1 1

  1. - C++ प्रोग्राम: ऐरे का उपयोग करके क्यू कार्यान्वयन

    ऐरे आधारित क्यू क्यू FIFO सिद्धांत का पालन करती है। ऐरे कार्यान्वयन में निश्चित आकार की सरणी और front/rear सूचकांक का उपयोग किया जाता है। C++ प्रोग्राम उदाहरण rear) { cout << Queue Underflow << endl; return; } else { cout << Element deleted: <<

  1. - C++ में लोकल क्लास (Local Class)

    लोकल क्लास क्या है? एक फ़ंक्शन के अंदर घोषित क्लास को लोकल क्लास कहते हैं क्योंकि यह उस फ़ंक्शन के लिए स्थानीय (local) होती है। बेसिक उदाहरण #include <iostream> using namespace std; void func() { class LocalClass { }; } int main() { return 0; } नियम और प्रतिबंध लोकल क्लास का नाम

  1. - C# में ऐरे क्लास क्या है

    Array क्लास C# में सभी ऐरे का बेस क्लास है। यह System नेमस्पेस में परिभाषित है। Array क्लास ऐरे के साथ काम करने के लिए विभिन्न प्रॉपर्टीज़ और मेथड्स प्रदान करती है। Array क्लास की प्रॉपर्टीज़ निम्नलिखित हैं - नीचे दी गई टेबल Array क्लास की सबसे अधिक उपयोग की जाने वाली प्रॉपर्टीज़ का वर्णन करती है।