1. #include <iostream>
#include <vector>
#include <string>
using namespace Std;
function declaration
void Output1 (vector<string> &);
void Output2 (vector<string> &);
int main ()
{
Vector<string>likes, dislikes; Create Vector<string> objects likes and dislikes
add element values for vector<string> Array object likes (favorite book, Music, film, PAINTINGS,ANIME,SPORT,SPORTSMAN,ETC)
Likes.push_back ("Favorite book");
Likes.push_back ("Music");
Likes.push_back ("film");
Likes.push_back ("paintings");
cout << "-----I like these-----" << Endl;
Call the child function output vector<string> Array object likes element value
OUTPUT1 (likes);
add element values for vector<string> array object dislikes
Dislikes.push_back ("anime");
Dislikes.push_back ("Sport");
Dislikes.push_back ("Sportsman");
cout << "-----I dislike these-----" << Endl;
Call the child function output vector<string> Array object dislikes element value
Output2 (dislikes);
Exchanging element values for vector<string> objects likes and dislikes
Likes.swap (dislikes);
cout << "-----I likes these-----" << Endl;
Call the child function output vector<string> Array object likes element value
Output2 (likes);
cout << "-----I dislikes these-----" << Endl;
Call the child function output vector<string> Array object dislikes element value
OUTPUT1 (dislikes);
return 0;
}
function implementation
Output the element value of the vector<string> array Object v with the following label
void Output1 (vector<string> &v) {
int i;
For (I=0;i<v.size (); i++)
cout<<v[i]<<endl;
}
function implementation
Output the element value of the vector<string> array Object V in an iterator
void Output2 (vector<string> &v) {
Vector<string>::iterator it;
For (It=v.begin (); It!=v.end (); it++)
cout<<*it<<endl;
}
2 (1)
#include <iostream>
using namespace Std;
int main () {
int * p;
The *p=9;//pointer-type variable p is not initialized, but instead points to a random address and cannot be assigned directly, but by creating a variable B, p points to B to complete the assignment.
int b=9;
p=&b;
cout<< "The value at p:" <<*p;
return 0;
}
2. (2)
#include <iostream>
using namespace Std;
int *fn1 () {
int *p=new int (5);
return p;
}
int main () {
int *a=fn1 ();
cout<< "The value of A is:" <<*a;
Delete A;
Dynamic space created with new, the last memory allocated to *p is not released, the A is set as a pointer variable, FN1 () is int* type, and after *a is created, it is released with delete
return 0;
}
C + + experiment Five