passing a vector to a function c++
C++
// C++ program to demonstrate how vectors
// can be passed by reference.
#include<bits/stdc++.h>
using namespace std;
// The vect is passed by reference and changes
// made here reflect in main()
void func(vector<int> &vect)
{
vect.push_back(30);
}
int main()
{
vector<int> vect;
vect.push_back(10);
vect.push_back(20);
func(vect);
for (int i=0; i<vect.size(); i++)
cout << vect[i] << " ";
return 0;
}
// C++ program to demonstrate that when vectors
// are passed to functions without &, a copy is
// created.
#include<bits/stdc++.h>
using namespace std;
// The vect here is a copy of vect in main()
void func(vector<int> vect)
{
vect.push_back(30);
}
int main()
{
vector<int> vect;
vect.push_back(10);
vect.push_back(20);
func(vect);
// vect remains unchanged after function
// call
for (int i=0; i<vect.size(); i++)
cout << vect[i] << " ";
return 0;
}
Also in C++:
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- c++ initialize array
- Category
- C++
- Title
- c++ rainbow text
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- inserting an element in an set c++
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- how to declare a function in c++
- Category
- C++
- Title
- hashset in c++
- Category
- C++
- Title
- char vector to string c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- remove from unordered_set c++
- Category
- C++
- Title
- accumulate in cpp
- Category
- C++
- Title
- dijkstra in c++
- Category
- C++
- Title
- c++ return multiple values
- Category
- C++
- Title
- c++ read file line by line
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- bfs in C++
- Category
- C++
- Title
- uepic games github
- Category
- C++
- Title
- min heap declaration in c++ stl
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- arduino delay millis
- Category
- C++
- Title
- dfs in c++
- Category
- C++
- Title
- set lower bound c++
- Category
- C++
- Title
- popualte an array c++
- Category
- C++
- Title
- printf c++
- Category
- C++
- Title
- c++ random numbers
- Category
- C++
- Title
- linear search in c++
- Category
- C++
- Title
- never gonna give you up
- Category
- C++
- Title
- creare array con c++
- Category
- C++
- Title
- loop c++
- Category
- C++
- Title
- Qt asynchronous HTTP request
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- tuple c++
- Category
- C++
- Title
- assegnare valori in c++
- Category
- C++
- Title
- program to know if a number is prime
- Category
- C++
- Title
- extends c++
- Category
- C++
- Title
- ceil c++;
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- write to file in C++
- Category
- C++
- Title
- Find the minimum difference between pairs in a simple path of tree C++
- Category
- C++
- Title
- c++ char to string
- Category
- C++
- Title
- iterar un map c++
- Category
- C++
- Title
- log base e synthax c++
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- c++ function to find minimum element in array
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- c++ map insert
- Category
- C++
- Title
- pbds in c++
- Category
- C++
- Title
- how to know the correct class of objects cpp
- Category
- C++
- Title
- array syntax in c++
- Category
- C++
- Title
- how to dynamically allocate an array c++
- Category
- C++
- Title
- vertical traversal of binary tree
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++
- Title
- struct c++
- Category
- C++
- Title
- coping 2d vector in cpp
- Category
- C++
- Title
- digitalwrite C++
- Category
- C++
- Title
- centos7 mlock2
- Category
- C++
- Title
- gta online
- Category
- C++
- Title
- check if intent has extras
- Category
- C++
- Title
- c++ looping through a vector
- Category
- C++
- Title
- floor() in c++
- Category
- C++
- Title
- Arrays hackerrank solution in c++
- Category
- C++
- Title
- c++ create object
- Category
- C++
- Title
- adding element in vector c++
- Category
- C++
- Title
- heap in cpp stl
- Category
- C++
- Title
- split 2d array into chunks in c++
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- what is meaning of 64 bit integer in c++
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- hashmap in c++
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- access last element in vector in c++
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- templates of templates c++
- Category
- C++
- Title
- Rectangle area hackerrank solution in c++
- Category
- C++
- Title
- array 2d to 1d
- Category
- C++
- Title
- set precision in c++
- Category
- C++
- Title
- how to cout in c++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- declaring vector c++
- Category
- C++
- Title
- Insert into vector C++
- Category
- C++
- Title
- c++ default constructor remove
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- c++ variable argument
- Category
- C++
- Title
- if esle in c++
- Category
- C++
- Title
- initialize 2d array c++
- Category
- C++
- Title
- opperanf >> c++
- Category
- C++
- Title
- how the theam are store in database
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- in c, is class uppercase or lowercase
- Category
- C++
- Title
- traverse a map
- Category
- C++
- Title
- c++ evaluate expression
- Category
- C++
- Title
- how to convert string to int c++
- Category
- C++
- Title
- pass ss tream as parameter c++
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- c++ stream string into fiel
- Category
- C++
- Title
- get elements of 2d array c++
- Category
- C++
- Title
- variant hold type
- Category
- C++
- Title
- find number of 1s in a binary cv::mat image
- Category
- C++
- Title
- can you use rand to read in from an external file inc++
- Category
- C++