How to add new element at the end of a vector in C++

2 Answers

0 votes
#include <iostream>
#include <vector>
 
void printVector(std::vector<int> const &v) {
    for (auto const &n: v) {
        std::cout << n << " ";
    }
}
  
int main()
{
	std::vector<int> v = { 5, 2, 7, 1, 9, 3, 6, 4 };
	
	v.insert(v.end(), 700);
	
	printVector(v);

	return 0;
}

  
  
  
  
/*
run:
  
5 2 7 1 9 3 6 4 700  
   
*/

 



answered Apr 10, 2020 by avibootz
0 votes
#include <iostream>
#include <vector>
  
void printVector(std::vector<int> const &v) {
    for (auto const &n: v) {
        std::cout << n << " ";
    }
}
   
int main()
{
    std::vector<int> v = { 5, 2, 7, 1, 9, 3, 6, 4 };
     
    v.push_back(700);
     
    printVector(v);
 
    return 0;
}
 
   
   
   
   
/*
run:
   
5 2 7 1 9 3 6 4 700 
    
*/

 



answered Apr 10, 2020 by avibootz

Related questions

3 answers 243 views
1 answer 222 views
1 answer 154 views
1 answer 203 views
1 answer 177 views
...