一* getline()-string
#include <iostream>
#include <string>
using namespace std;
void print(string str) { cout << str; }
void printstr(string str, int n = 1) {
for (int i = 0; i < n; ++i)
print(str);
}
int main() {
string str;
int n=5;
cout << "Enter what you want to say: ";
getline(cin, str);
printstr(str);
cout << endl;
printstr(str, n);
return 0;
}
二 strcpy()待修改
//待修改
#include <iostream>
using namespace std;
struct CandyBar {
char* brand;
double weight;
int energy;
};
void myCandy(CandyBar& candy,const char* brand = "Milennium Munch",const double weight=2.85,const int energy=350) {
strcpy_s(candy.brand,strlen(brand)+1, brand);//0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突。 13
//strcpy(candy.brand, brand);//C4996 'strcpy': unsafe. strcpy_s instead.To disable deprecation, use _CRT_SECURE_NO_WARNINGS 11
//candy.brand = brand; //不能将const char*类型的值分配到char*类型的实体 12
candy.weight = weight;
candy.energy = energy;
cout << "Candy Brand: " << candy.brand
<< "\nCandy Weight: " << candy.weight<<" g"
<< "\nCandy Energy: " << candy.energy<<" cal\n";
}
int main() {
CandyBar candy;
myCandy(candy);
return 0;
}
//string
#include <iostream>
#include <string>
using namespace std;
struct CandyBar {
string brand;
double weight;
int energy;
};
void myCandy(CandyBar& candy,const string brand = "Milennium Munch",const double weight=2.85,const int energy=350) {
candy.brand = brand;
candy.weight = weight;
candy.energy = energy;
cout << "Candy Brand: " << candy.brand
<< "\nCandy Weight: " << candy.weight<<" g"
<< "\nCandy Energy: " << candy.energy<<" cal\n";
}
int main() {
CandyBar candy;
myCandy(candy);
return 0;
}
三* str.length()
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string toupper(string& str) {
for (int i = 0;i < str.length();++i)
str[i] = toupper(str[i]);
return str;
}
int main() {
cout << "Enter a string (q to quit): ";
string str;
getline(cin, str);
while (str[0] != 'q') {
if (str.length() != 1) {
cout << toupper(str)<<endl
<< "Next string (q to quit): ";
getline(cin, str);
}
else
break;
}
cout << "Bye.\n";
return 0;
}
四* 重载函数(特征标)
#define _CRT_SECURE_NO_WARNINGS//when not strcpy_s()
#include<iostream>
using namespace std;
#include<cstring>//strlen(),strcpy()
struct stringy {
char* str;
int ct;//length of string(no '\0')
};
void set(stringy& bea,const char* test) {//bea为引用参数
bea.str = new char [strlen(test) + 1];//分配空间以保存test
strcpy(bea.str, test);//设置Beany的str成员指向新块,将testing复制到新块,并设置beay的ct成员
}
void show(const stringy& bea,const int n=1) {
for (int i = 0; i < n; ++i)
cout << bea.str;
}
void show(const string& str, const int n=1) {
for (int i = 0; i < n; ++i)
cout << str;
}
int main() {
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany, testing);
show(beany); cout << endl;
show(beany, 2); cout << endl;
testing[0] = 'D';
testing[1] = 'u';
show(testing); cout << endl;
show(testing, 3); cout << endl;
show("Done!\n");
return 0;
}
五* 模板函数
#include <iostream>
const int size = 5;
template <typename T>
T max5(T a[]) {
T b=0;
for (int i = 0; i < size-1; ++i)
a[i] > a[i + 1]?b = a[i]:b = a[i + 1];
return b;
}
int main() {
int inte[size] = { 1,0,3,4,5 };
double dou[size] = { 12.0,11.2,23.5,78.6,1.1 };
std::cout<<max5(inte, size)<<std::endl<<max5(dou,size)<<std::endl;
}
六* 具体化待修改
#include <iostream>
const int sizeinte = 6,sizedou=4;
template <typename T>
T maxn(T a[], int n) {
T b = 0;
for (int i = 0; i < n - 1; ++i)
a[i] >= a[i + 1] ? b = a[i] : b = a[i + 1];
return b;
}
template <>char* maxn(char* pt[],int& n) {//没有与指定类型匹配的 函数模板 "maxn"实例
char* b;
for (int i = 0; i < n - 1; ++i)
strlen(pt[i]) >= strlen(pt[1 + 1]) ? b = pt[i] : b = pt[i + 1];
return b;
}
int main() {
int inte[sizeinte] = { 1,0,3,4,5,5 };
double dou[sizedou] = { 12.0,11.2,23.5,1.1 };
std::cout << maxn(inte, sizeinte) << std::endl << maxn(dou, sizedou) << std::endl;
}
七
//tempover.cpp -- template overloading
#include <iostream>
using namespace std;
template <typename T>
void SumArray(T Arr[], int n);
template <typename T>
void SumArray(T* array[], int n);
struct debts {
char name[50];
double amount;
};
int main() {
int things[6] = { 13,31,103,301,310,130 };
struct debts mr_E[3] = {
{"Ima Wolfe",2400.0},
{"Ura Foxe",1300.0},
{"Iby Stout",1800.0}
};
double* pd[3];
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;
cout << "Listinng Mr.E's debts:\n";
SumArray(things, 6);
cout << "Listing Mr.E's debts:\n";
SumArray(pd, 3);
return 0;
}
template <typename T>
void SumArray(T arr[], int n) {
cout << "Template A\n";
T sum=0;
for (int i = 0; i < n; i++)
sum+=arr[i];
cout << sum<<endl;
}
template <typename T>
void SumArray(T* arr[], int n) {
cout << "template B\n";
T sum = 0;
for (int i = 0; i < n; i++)
sum += *arr[i];
cout<<sum<<endl;
}