Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
How to Access Vector Methods from Pointer to Vector in C++?
Next article icon

How to Access Vector Methods from Pointer to Vector in C++?

Last Updated : 17 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, we can create a pointer that points to the object of vector type. In this article, we will learn how to access member functions of std::vector from a pointer to the vector in C++.

Example

Input:
vec = {1, 2, 3, 4}
vector<int>* ptr = vec

Output:
ptr->at(2): 3

Accessing std::vector Methods using Pointers in C++

We can access the member functions of a std::vector from a pointer to the vector in a similar way as we access member functions from an object, but with a difference that we have to use arrow operator (->) instead of the dot operator (.) to access its members (both functions and variables).

Syntax to Access Member Functions From Pointer

pointerName->memberFunction(args);

Here,

  • pointerName is the name of the pointer to the vector.
  • memberFunction is the name of the member function that we want to access.
  • args is the arguments if any

C++ Program to Access Member Functions From Pointer to a Vector

The below program demonstrates how we can access member functions from a pointer to a vector in C++.

C++
// C++ program to access member function from pointer to a // vector #include <iostream> #include <vector> using namespace std;  int main() {     // Declaring a pointer to a vector     vector<int>* vectorPtr = new vector<int>();      // Adding elements to the vector     vectorPtr->push_back(10);     vectorPtr->push_back(20);     vectorPtr->push_back(30);      // Accessing and printing data using pointer     cout << "Elements of vector: ";     for (int i = 0; i < vectorPtr->size(); ++i) {         cout << (*vectorPtr)[i] << " ";     }      return 0; } 

Output
Elements of vector: 10 20 30 

Time Complexity: O(1)
Auxilliary Space: O(1)

 

 


Next Article
How to Access Vector Methods from Pointer to Vector in C++?

A

akshitsaxenaa09
Improve
Article Tags :
  • C++ Programs
  • C++
  • cpp-pointer
  • cpp-vector
  • CPP Examples
Practice Tags :
  • CPP

Similar Reads

    How to Access Vector Element Using Iterator in C++?
    In C++, vectors are container that works like a dynamic array. STL provides iterators for the traversal of these containers. In this article, we will learn how to access an element in a vector using an iterator in C++. Example Input: myVector = {1, 5, 8, 1, 3, 4} Output: myVector value at index 5 us
    2 min read
    How to Access the Last Element in a Vector in C++?
    Given a vector of n elements, the task is to access the last element in C++.ExamplesInput: v = {11, 23, 9, 7};Output: 7Explanation: Since 7 is the last element of the vector.Input: v = {1, 3, 11, 52};Output: 52Explanation: Since 52 is the last element of the vector.Following are the different ways f
    2 min read
    How to Create Vectors of Unique Pointers in C++?
    In C++, the vector is defined as a dynamic array that can grow or shrink in size. Vectors of unique pointers are commonly used to manage the collections of dynamically allocated objects as they combine the dynamic resizing capability of vectors with the automatic memory management provided by unique
    2 min read
    How to Access First Element in a Vector Using Iterator in C++?
    In C++, vectors are containers similar to arrays but unlike arrays, vectors can resize themselves during the runtime. These vectors provide iterators to access their elements. In this article, we will learn how to access the first element in a vector using an iterator in C++. Example Input:myVector
    2 min read
    How to Access the First Element of a Vector in C++?
    In this article, we will learn how to access the first element of vector in C++.The most efficient way to access the first element of vector is by using vector front() function. Let’s take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences