Sie sind auf Seite 1von 20

1.

Queue
// CPP program to illustrate
// Implementation of push() function
#include <iostream>
#include <queue>
using namespace std;

int main()
{
// Empty Queue
queue<int> myqueue;
myqueue.push(0);
myqueue.push(1);
myqueue.push(2);

// Printing content of queue


while (!myqueue.empty()) {
cout << ' ' << myqueue.front();
myqueue.pop();
}
}

Output:
1 1 2

Push () function is used to insert an element at the back of the queue. The
element is added to the queue container and the size of the queue is increased by
1.

2. Queue
// CPP program to illustrate
// Implementation of pop () function
#include <iostream>
#include <queue>
using namespace std;
int main()
{
// Empty Queue
queue<int> myqueue;
myqueue.push(0);
myqueue.push(1);
myqueue.push(2);
// queue becomes 0, 1, 2
myqueue.pop();
myqueue.pop();
// queue becomes 2
// Printing content of queue
while (!myqueue.empty()) {
cout << ' ' << myqueue.front();
myqueue.pop();
}
}
Output:
2

Pop () function is used to remove an element from the front of the queue (oldest
element in the queue). The element is removed to the queue container and the
size of the queue is decreased by 1.

3. Queue

// CPP program to illustrate


// Application of push() and pop() function
#include <iostream>
#include <queue>
using namespace std;

int main()
{
int c = 0;
// Empty Queue
queue<int> myqueue;
myqueue.push(5);
myqueue.push(13);
myqueue.push(0);
myqueue.push(9);
myqueue.push(4);
// queue becomes 5, 13, 0, 9, 4

// Counting number of elements in queue


while (!myqueue.empty()) {
myqueue.pop();
c++;
}
cout << c;
}

Output:
5
Given a number of integers, add them to the queue and find the size of the queue
without using size function.

4. Queue

// CPP program to illustrate


// Implementation of empty() function
#include <iostream>
#include <queue>
using namespace std;

int main()
{
queue<int> myqueue;
myqueue.push(1);

// Queue becomes 1
if (myqueue.empty()) {
cout << "True";
}
else {
cout << "False";
}
return 0;
}

Output:
False
Empty () function is used to check if the queue container is empty or not

5. Queue
// CPP program to illustrate
// Application of empty() function
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    int sum = 0;
    queue<int> myqueue;
    myqueue.push(1);
    myqueue.push(8);
    myqueue.push(3);
    myqueue.push(6);
    myqueue.push(2);
 
    // Queue becomes 1, 8, 3, 6, 2
    while (myqueue.size() > 0) {
        sum = sum + myqueue.front();
        myqueue.pop();
    }
    cout << sum;
    return 0;
}
Output:
20
Given a queue of integers, find the sum of the all the integers.

6. Queue
// CPP program to illustrate
// Implementation of size() function
#include <iostream>
#include <queue>
using namespace std;

int main()
{
int sum = 0;
queue<int> myqueue;
myqueue.push(1);
myqueue.push(8);
myqueue.push(3);
myqueue.push(6);
myqueue.push(2);

// Queue becomes 1, 8, 3, 6, 2

cout << myqueue.size();

return 0;
}

Output:
5
Size () function is used to return the size of the list container or the number of elements in the
list container.
7. Queue
#include <iostream>  
#include <queue>  
int main()  
{  
    std::queue<int> newqueue;  
    newqueue.push(24);  
    newqueue.push(80);  
    newqueue.front () +=20;  
    std::cout <<"newqueue.front() is modified to " << newqueue.front();  
    return 0;  
}  

Output:

newqueue.front() is modified to 44

The function is used to access the front element of the queue. The element plays a very important role
as all the deletion operations are performed at the front element.

8. Queue
#include <iostream>  
#include <queue>  
int main()  
{  
    std::queue<int> newqueue;  
    newqueue.push(24);  
    newqueue.push(80);  
    newqueue.back () += newqueue.front();  
    std::cout <<"newqueue.back() is modified to" << newqueue.back ();  
    return 0;  
}  

Output:

newqueue.back() is modified to 104

The function is used to access the rear element of the queue. The element plays a very important
role as all the insertion operations are performed at the rear element.
9. Queue
// queue::swap
#include <iostream> // std::cout
#include <queue> // std::queue

int main ()
{
std::queue<int> foo,bar;
foo.push (10); foo.push(20); foo.push(30);
bar.push (111); bar.push(222);

foo.swap(bar);

std::cout << "size of foo: " << foo.size() << '\n';


std::cout << "size of bar: " << bar.size() << '\n';

return 0;
}

Output:

size of foo: 2
size of bar: 3
The function is used for interchanging the contents of two containers in reference.
1. Stack
#include <iostream>
#include <stack>
int main()
{
std::stack<int> newstack;
int sum=0;
for (int j=1; j<=10; j++)
newstack.push(j);
while (!newstack.empty ())
{
sum += newstack.top ();
newstack.pop ();
}
std::cout << "Result is: " << sum;
return 0;
}
Output:

Result is: 55

Stack empty () function is used for testing whether the container is empty or not.

2. Stack
#include <iostream>  
#include <stack>  
using namespace std;  
int main()  
{  
    std::stack<int> newstack;  
    std::cout << "0. size: "<< newstack.size();  
    for(int j=0; j<5; j++)  
    newstack.push(j);  
    cout<<"\n";  
    std::cout<<"1. size: " << newstack.size();  
    newstack.pop();  
    cout<<"\n";  
    std::cout<<"2. size: "<< newstack.size();  
    return 0;  
}  

Output:

0. size: 0
1. size: 5
2. size: 4

The function returns the size of the stack container, which is a measure of the
number of elements stored in the stack.

3. Stack
#include <iostream>  
#include <stack>  
int main()  
{  
    std::stack<int> newstack;  
    newstack.push(24);  
    newstack.push(80);  
    newstack.top () +=20;  
    std::cout <<"newstack.top() is modified to" <<newstack.top ();  
    return 0;  
}  

Output:

newstack.top() is modified to 100

The function is used to access the top element of the stack. The element plays a very important
role as all the insertion and deletion operations are performed at the top element.
4. Stack
#include <iostream>  
#include <stack>  
int main()  
{  
         std::stack<int> newstack;  
         for(int j= 0; j<5; j++)  
         newstack.push(j);  
         std::cout << "Poping the elements out of the stack??.";  
         while (!newstack.empty () )  
         {  
       std::cout<<" " << newstack.top ();  
        newstack.pop();  
    }  
      
  
std::cout<<"\n";  
return 0;  
}  

Output:

Poping the elements out of the stack..... 4 3 2 1 0

The function is used for the insertion of a new element at the top of the stack.

5. Stack
#include <iostream>  
#include <stack>  
int main()  
{  
    std::stack<int> newstack;   
    newstack.push(11);  
    newstack.push(22);  
    newstack.push(33);  
    newstack.push(44);  
    std::cout << "Popping out elements?";  
    newstack.pop();  
    newstack.pop();  
    while (!newstack.empty () )  
    {  
        std::cout << " "<< newstack.top();  
        newstack.pop();  
    }  
    std::cout<<"\n";  
    return 0;  
}  

Output:

Popping out elements... 22 11

The function is used for the deletion of element; the element in the stack is deleted from the
top.

6. Stack
#include <iostream>
using namespace std;
int main()
{
int a=5, b=10;
cout<<"Before swap a= "<<a<<" b= "<<b<<endl;
a=a*b; //a=50 (5*10)
b=a/b; //b=5 (50/10)
a=a/b; //a=10 (50/5)
cout<<"After swap a= "<<a<<" b= "<<b<<endl;
return 0;
}

Output:

Before swap a= 5 b= 10
After swap a= 10 b= 5

The function is used for interchanging the contents of two containers in reference.
7. Stack
// CPP program to illustrate

// Implementation of push() function

#include <iostream>

#include <stack>

using namespace std;

int main()

// Empty stack

stack<int> mystack;

mystack.push(0);

mystack.push(1);

mystack.push(2);

// Printing content of stack

while (!mystack.empty()) {

cout << ' ' << mystack.top();

mystack.pop();

Output:
2 1 0
Push () function is used to insert an element at the top of the stack. The element is
added to the stack container and the size of the stack is increased by 1.
1. Array
// arrays example
#include <iostream>
using namespace std;

int foo [] = {16, 2, 77, 40, 12071};


int n, result=0;

int main ()
{
for ( n=0 ; n<5 ; ++n )
{
result += foo[n];
}
cout << result;
return 0;
}
Output:
12206
Following the previous examples in which foo had 5 elements and each of those
elements was of type int, the name which can be used to refer to each element is the
following:

2. Array
// arrays as parameters
#include <iostream>
using namespace std;

void printarray (int arg[], int length) {


for (int n=0; n<length; ++n)
cout << arg[n] << ' ';
cout << '\n';
}

int main ()
{
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray,3);
printarray (secondarray,5);
}
Output:
5 10 15
2 4 6 8 10
To accept an array as parameter for a function, the parameters can be declared as
the array type, but with empty brackets, omitting the actual size of the array.

3. Array
#include <iostream>
using namespace std;

int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";

// Storing 5 number entered by user in an array


// Finding the sum of numbers entered
for (int i = 0; i < 5; ++i)
{
cin >> numbers[i];
sum += numbers[i];
}
cout << "Sum = " << sum << endl;

return 0;
}

Output:
Enter 5 numbers: 2
3
4
6
8
Sum = 23
C++ program to store and calculate the sum of 5 numbers entered by the user using
arrays.
4. Array
#include <iostream>
using namespace std;
int main()
{
int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array
//traversing array
for (int i = 0; i < 5; i++)
{
cout<<arr[i]<<"\n";
}
}

Output

10
0
20
0
30

Single Dimensional Array - Let's see a simple example of C++ array, where we are
going to create, initialize and traverse array.

5. Array

#include <iostream>  
using namespace std;  
void printArray(int arr[5]);  
int main()  
{  
        int arr1[5] = { 10, 20, 30, 40, 50 };    
        int arr2[5] = { 5, 15, 25, 35, 45 };    
        printArray(arr1); //passing array to function    
        printArray(arr2);  
}  
void printArray(int arr[5])  
{  
    cout << "Printing array elements:"<< endl;  
    for (int i = 0; i < 5; i++)  
    {  
                   cout<<arr[i]<<"\n";    
    }  
}  

Output:

Printing array elements:


10
20
30
40
50
Printing array elements:
5
15
25
35
45
Let's see an example of C++ function which prints the array elements

6. Array

#include <iostream>  
using namespace std;  
void  printMax(int arr[5]);  
int main()  
{  
        int arr1[5] = { 25, 10, 54, 15, 40 };    
        int arr2[5] = { 12, 23, 44, 67, 54 };    
        printMax(arr1); //Passing array to function  
         printMax(arr2);   
}  
void  printMax(int arr[5])  
{  
    int max = arr[0];    
        for (int i = 0; i < 5; i++)    
        {    
            if (max < arr[i])    
            {    
                max = arr[i];    
            }    
        }    
        cout<< "Maximum element is: "<< max <<"\n";    
}  

Output:

Minimum element is: 54


Minimum element is: 67

Let's see an example of C++ array which prints maximum number in an array using
function.
7. Array
#include <iostream>  
using namespace std;  
int main()  
{  
  int test[3][3] =  
    {  
        {2, 5, 5},  
        {4, 0, 3},  
        {9, 1, 8}  };  //declaration and initialization    
    //traversal    
    for(int i = 0; i < 3; ++i)  
    {  
        for(int j = 0; j < 3; ++j)  
        {  
            cout<< test[i][j]<<" ";  
        }  
        cout<<"\n"; //new line at each row   
    }  
    return 0;  
}  

Output:"

255
403
918

Let's see a simple example of multidimensional array which initializes array at the time
of declaration.
8. Array
#include <iostream>

using namespace std;

int main(){

// initializing the array

int arr[2][3][2] = {

{ {1,-1}, {2,-2}, {3,-3} },

{ {4,-4}, {5,-5}, {6,-6} }

};

// displaying array values

for (int x = 0; x < 2; x++) {

for (int y = 0; y < 3; y++) {

for (int z = 0; z < 2; z++) {

cout<<arr[x][y][z]<<" ";

return 0;

Output:

1 -1 2 -2 3 -3 4 -4 5 -5 6 -6
This way of initializing is preferred as you can visualize the rows and columns here.
9. Array
#include <iostream>
using namespace std;

int main(){
int arr[2][3] = {{11, 22, 33}, {44, 55, 66}};
for(int i=0; i<2;i++){
for(int j=0; j<3; j++){
cout<<"arr["<<i<<"]["<<j<<"]: "<<arr[i][j]<<endl;
}
}
return 0;
}

Output:

arr[0][0]: 11
arr[0][1]: 22
arr[0][2]: 33
arr[1][0]: 44
arr[1][1]: 55
arr[1][2]: 66

This way of initializing is preferred as you can visualize the rows and columns here.

10. Array
#include <iostream>

using namespace std;

/* This function adds the corresponding

 * elements of both the arrays and

 * displays it.

 */

void sum(int arr1[], int arr2[]){


int temp[5];

for(int i=0; i<5; i++){

temp[i] = arr1[i]+arr2[i];

cout<<temp[i]<<endl;

int main(){

int a[5] = {10, 20, 30, 40 ,50};

int b[5] = {1, 2, 3, 4, 5};

//Passing arrays to function

sum(a, b);

return 0;

Output:

11
22
33
44
55

We are passing two arrays a & b to the function sum (). This function adds the
corresponding elements of both the arrays and displays them.

Das könnte Ihnen auch gefallen