Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to use pointer to vector in C++

6 Answers

0 votes
#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::string;

void print(const vector<string> *const p) {
	for (vector<string>::const_iterator it = (*p).begin(); it != (*p).end(); it++)
		cout << *it << endl;
}

int main()
{
	vector<string> *v = new vector<string>(3);

	v->at(0) = "c++";
	v->at(1) = "php";
	v->at(2) = "java";

	print(v);

	delete v;

	return 0;
}


/*
run:

c++
php
java

*/

 



answered May 25, 2018 by avibootz
edited May 25, 2018 by avibootz
0 votes
#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::string;

int main()
{
	vector<string> *v = new vector<string>(3);

	v->at(0) = "c++";
	v->at(1) = "php";
	v->at(2) = "java";

	cout << v->operator[](0) << endl;
	cout << v->operator[](1) << endl;
	cout << v->operator[](2) << endl;

	delete v;

	return 0;
}


/*
run:

c++
php
java

*/

 



answered May 25, 2018 by avibootz
edited May 25, 2018 by avibootz
0 votes
#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::string;

int main()
{
	vector<string> *v = new vector<string>(3);

	v->at(0) = "c++";
	v->at(1) = "php";
	v->at(2) = "java";

	for (int i = 0; i < v->size(); i++)
		cout << v->operator[](i) << endl;

	delete v;

	return 0;
}


/*
run:

c++
php
java

*/

 



answered May 25, 2018 by avibootz
edited May 25, 2018 by avibootz
0 votes
#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::string;

int main()
{
	vector<string> *v = new vector<string>(3);

	(*v)[0] = "c++";
	(*v)[1] = "php";
	(*v)[2] = "java";

	for (int i = 0; i < v->size(); i++)
		cout << (*v)[i] << endl;

	delete v;

	return 0;
}


/*
run:

c++
php
java

*/

 



answered May 25, 2018 by avibootz
0 votes
#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::string;

int main()
{
	vector<string> *v = new vector<string>(3);

	(*v)[0] = "c++";
	(*v)[1] = "php";
	(*v)[2] = "java";

	for (auto s : *v)
		cout << s << endl;

	delete v;

	return 0;
}


/*
run:

c++
php
java

*/

 



answered May 25, 2018 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 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
   
    printVector(v);
     
    int *p = v.data();
 
    *p = 99;
    ++p;
    *p = 7513;
    *(p + 2) = 238;
    p[5] = 8942; 
     
    std::cout << std::endl;
     
    printVector(v);
}
   
   
   
/*
run:
   
1 2 3 4 5 6 7 8 9 
99 7513 3 238 5 6 8942 8 9 
    
*/

 



answered May 7, 2024 by avibootz

Related questions

1 answer 191 views
1 answer 106 views
1 answer 143 views
1 answer 102 views
1 answer 117 views
117 views asked May 13, 2021 by avibootz
...