C + + experiment Five

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.