Answers for "vector in function c++"

C++
3

vector functions c++

/*Member functions*/
                 Iterators
 -----------------------------------------
 begin | Returns an iterator to the beginning
 end   | Returns an iterator to the end
  
                 Capacity
 -----------------------------------------
 empty    | Checks whether the container is empty
 size     | Returns the number of elements
 reserve  | Reserves storage
 capacity | Returns the number of elements that can be held in currently allocated storage
     
              Element access
 -----------------------------------------
 at	     	| Access specified element with bounds checking
 front  	| Access the first element
 back   	| Access the last element
 operator[] | Access specified element
    
                 Modifiers
 -----------------------------------------
 clear        | Clears the contents
 insert       | Inserts elements
 emplace      | Constructs element in-place
 erase        | Erases elements
 push_back    | Adds an element to the end
 emplace_back | Constructs an element in-place at the end  
 pop_back     | Removes the last element        
 resize       | Changes the number of elements stored     
 swap         | Swaps the contents
  
  *Notes*
  - https://en.cppreference.com/w/cpp/container/vector
  - https://www.geeksforgeeks.org/vector-in-cpp-stl/
  - https://www.tutorialspoint.com/cpp_standard_library/vector.htm
Posted by: Guest on May-13-2021
1

declare vectors c++

vector<int> vec;
//Creates an empty (size 0) vector
 

vector<int> vec(4);
//Creates a vector with 4 elements.

/*Each element is initialised to zero.
If this were a vector of strings, each
string would be empty. */

vector<int> vec(4, 42);

/*Creates a vector with 4 elements.
Each element is initialised to 42. */


vector<int> vec(4, 42);
vector<int> vec2(vec);

/*The second line creates a new vector, copying each element from the
vec into vec2. */
Posted by: Guest on May-25-2020
2

vector in c++

vector<int> g1; 
  
    for (int i = 1; i <= 5; i++) 
        g1.push_back(i); 
  
    cout << "Output of begin and end: "; 
    for (auto i = g1.begin(); i != g1.end(); ++i) 
        cout << *i << " "; 
  
    cout << "nOutput of cbegin and cend: "; 
    for (auto i = g1.cbegin(); i != g1.cend(); ++i) 
        cout << *i << " "; 
  
    cout << "nOutput of rbegin and rend: "; 
    for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir) 
        cout << *ir << " ";
Posted by: Guest on February-02-2021
0

vector operations in c++

#include <iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int n;
    cin>>n;
    vector<int>vec(n);
    for(int i=0;i<n;i++)
    {
        cin>>vec[i];
    }
    cout<<"----------"<<endl;
    for(int i=0;i<n;i++)
    {
      cout<<vec[i]<<" ";
    }
    vec.push_back(100);
    vec.push_back(200);
    cout<<endl;
    auto it=vec.begin();
    vec.insert(it,3,1000);
    for(vector<int>::iterator it1=vec.begin();it1!=vec.end();it1++)
    {
        cout<<*it1<<" ";
    }
    cout<<endl;
    vector<int>vec2;
    vec2.push_back(15);
    vec2.push_back(25);
    vec2.push_back(35);
    vec.insert(it,vec2.begin(),vec2.end());
    for(vector<int>::iterator it1=vec.begin();it1!=vec.end();it1++)
    {
        cout<<*it1<<" ";
    }
    cout<<endl;
    vec.pop_back();
    vec.pop_back();
    for(vector<int>::iterator it1=vec.begin();it1!=vec.end();it1++)
    {
        cout<<*it1<<" ";
    }
    cout<<endl;
    int k;
    cin>>k;
    cout<<endl;
    vector<int>::iterator its=vec.begin()+k-1;
    int s=vec.size();
    cout<<s<<endl;
    vec.erase(its);
    for(vector<int>::iterator it1=vec.begin();it1!=vec.end();it1++)
    {
        cout<<*it1<<" ";
    }
    

    return 0;
}
Posted by: Guest on June-04-2021

Browse Popular Code Answers by Language