没有合适的资源?快使用搜索试试~ 我知道了~
第四章补充作业:1
需积分: 0 0 下载量 197 浏览量
2022-08-08
23:11:01
上传
评论
收藏 152KB DOCX 举报
温馨提示
试读
13页
第四章补充作业:1
资源详情
资源评论
资源推荐
第四章补充作业:
(1)
源代码:
// main.cpp
// 1
// Created by mac on 16/3/29.
// Copyright © 2016年 mac. All rights reserved.
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
// insert code here...
//std::cout << "Hello, World!";
string str="Xi’an Institute of Posts and
Telecommunications is co-designed and implemented by the
People’s Government of Shaanxi Province and the Ministry
of Industry and Information Technology.The Institute is
located in Xi’an, a historic city in Northwest China,
famous for its magnificent ancient culture";
string str1="Institute";
string rstr="University";
string::size_type pfd=str.find(str1);
while (pfd!=string::npos) {
str.replace(pfd, rstr.length()-1, rstr);
pfd=str.find(str1);
}
cout<<str<<endl;
return 0;
}
运行结果:
Xi’an University of Posts and Telecommunications is co-designed and
implemented by the People’s Government of Shaanxi Province and the
Ministry of Industry and Information Technology.
The University is located in Xi’an, a historic city in Northwest
China, famous for its magnificent ancient culture
Program ended with exit code: 0
(2):
源代码:
// main.cpp
// 2
// Created by mac on 16/3/29.
// Copyright © 2016年 mac. All rights reserved.
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
// insert code here...
//std::cout << "Hello, World!\n";
string string1("The airplane landed on time!");
cout<<string1.substr(7,5)<<endl;
return 0;
}
运行结果:
plane
Program ended with exit code: 0
(3):
源代码:
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
string first( "one" );
string second( "two" );
cout << "Before swap:\n first: " << first << "\nsecond:
" << second;
first.swap( second );
cout << "\n\nAfter swap:\n first: " << first
<< "\nsecond: " << second << endl;
return 0;
}
运行结果:
Before swap:
first: one
second: two
After swap:
first: two
second: one
Program ended with exit code: 0
(4):
源代码:
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::boolalpha;
#include <string>
using std::string;
void printStatistics( const string & );
int main()
{
string string1;
cout << "Statistics before input:\n" << boolalpha;
printStatistics( string1 );
cout << "\n\nEnter a string: ";
cin >> string1;
cout << "The string entered was: " << string1;
cout << "\nStatistics after input:\n";
printStatistics( string1 );
cin >> string1;
cout << "\n\nThe remaining string is: " << string1 <<
endl;
printStatistics( string1 );
string1 +=
"1234567890abcdefghijklmnopqrstuvwxyz1234567890";
cout << "\n\nstring1 is now: " << string1 << endl;
printStatistics( string1 );
string1.resize( string1.length() + 10 );
cout << "\n\nStats after resizing by (length +
10):\n";
printStatistics( string1 );
cout << endl;
return 0;
}
void printStatistics( const string &stringRef )
{
cout << "capacity: " << stringRef.capacity() << "\nmax
size: "
剩余12页未读,继续阅读
明儿去打球
- 粉丝: 15
- 资源: 327
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0