#include<iostream>#include<cmath>usingnamespacestd;
int main()
{
double input;
double result;
cin >> input;
result = sqrt(input);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << result << endl;
system("pause");
return0;
}
exit函数:
exit函数定义在cstdlib库中
#include <iostream>#include <cstdlib>usingnamespacestd;
int main()
{
cout << "Hello Out There!\n";
exit(1);
cout << "This statement is pointless,\n"
<< "because it will never be executed.\n"
<< "This is just a toy program to illustrate exit.\n";
return0;
}
#include<iostream>usingnamespacestd;
int fun(double i);
int main()
{
double input;
cin >> input;
cout << fun(input) << endl;
system("pause");
return0;
}
int fun(double i) {
returnstatic_cast<int>(i + 0.5);
}
struct与字符串的拼接
#include<iostream>#include<string>#include<sstream>usingnamespacestd;
struct Student
{
string name;
int age;
double grade;
};
int main()
{
Student students[10];
stringstream sst;
for (int i = 0; i < 10; i++)
{
//清空上次的内容
sst.str("");
sst << (i + 1);
//拼接字符串
students[i].name = "lotus " + sst.str();
students[i].age = rand() % 30;
students[i].grade = rand() % 100;
}
for (int i = 0; i < 10; i++)
{
cout << students[i].name << "\t" << students[i].age << "\t" << students[i].grade << endl;
}
system("pause");
return0;
}
vector
#include<iostream>#include<vector>usingnamespacestd;
int main()
{
vector<int> data;
for (int i = 0; i < 1000;i++)
data.push_back(rand() % 1000);
cout << "the total size of the vector " << data.size() << endl;
for (int i = 0; i < data.size(); i++)
{
cout << data[i] << "\t";
if (i>10 && i / 10 == 0)
{
cout << endl;
}
}
system("pause");
return0;
}