指针、数组、main参数 、读写文件、简单内存管理
#include <iostream>
#include <cstddef>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int (*fp)(int); //函数指针
int next(int n)
{
return n + 1;
}
// these two statements are equivalent
fp = &next;
fp = next;
// these two statements are equivalent
i = (*fp)(i);
i = fp(i);
typedef double (*analysis_fp)(const vector<Student_info>&);
// get_analysis_ptr returns a pointer to an analysis function
analysis_fp get_analysis_ptr(); //返回函数指针的函数
//也可以直接 double (*get_analysis_ptr())(const vector<Student_info>&);
//如果调用get_analysis_ptr() 然后对其结果进行解引用 得到一个函数
//得到的函数带有一个参数const vector<Student_info>&
//返回值类型为double
//一个find_if 实现
template<class In, class Pred>
In find_if(In begin, In end, Pred f)
{
while (begin != end && !f(*begin))
++begin;
return begin;
}
bool is_negative(int n)
{
return n < 0;
}
//得到vector<int> v中第一个值为负数的元素位置
vector<int>::iterator i = find_if(v.begin(), v.end(), is_negative);
double coords[3];
const size_t NDim = 3;
double coords[NDim]; //NDim是个常量 编译器可知
vector<double> v;
copy(coords, coords + NDim, back_inserter(v)); //数组copy到vector中
vector<double> v(coords, coords + NDim); //用数组构造vector
// Example implementation of standard-library function
size_t strlen(const char* p)
{
size_t size = 0;
while (*p++ != '\0')
++size;
return size;
}
const char hello[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
string s(hello);
string s("Hello");
string s(hello, hello + strlen(hello));
//If the grade is at least 97 94 90 87 84 80 77 74 70 60 0
//then the letter grade is A+ A A- B+ B B- C+ C C- D F
string letter_grade(double grade)
{
// range posts for numeric grades
//static 表示只初始化一次
static const double numbers[] = {
97, 94, 90, 87, 84, 80, 77, 74, 70, 60, 0
};
// names for the letter grades
static const char* const letters[] = {
"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "F"
};
// compute the number of grades given the size of the array
// and the size of a single element
static const size_t ngrades = sizeof(numbers)/sizeof(*numbers);
// given a numeric grade, find and return the associated letter grade
for (size_t i = 0; i < ngrades; ++i)
{
if (grade >= numbers[i])
return letters[i];
}
return "?\?\?"; //输出 ???
}
int main(int argc,char **argv) //char *argv[] 等价char **argv 但只有在参数列表中合法
{ //main参数 argc参数个数 第一个为exe的名字 后面为输入的参数
//cmd中输入 1.exe Hello Word!(或1 Hello Word!) 输出Hello Word!
if(argc>1)
{
int i;
for(i=1;i<argc-1;i++)
cout<<argv[i]<<" ";
cout<<argv[i]<<endl;
}
return 0;
}
int main() //读写文件
{
ifstream infile("in");
ofstream outfile("out");
string s;
while (getline(infile, s))
outfile << s << endl;
return 0;
}
//if file is a string variable that contains the name of a file that we want to read,
//we can create an ifstream object that will read it by defining it as
ifstream infile(file.c_str()); //string To char *
// this function deliberately yields an invalid pointer.
// it is intended as a negative example—don't do this!
int* invalid_pointer()
{
int x;
return &x; // instant disaster! x在函数返回时 已经释放
}
// This function is completely legitimate.
int* pointer_to_static()
{
static int x;
return &x; //合法 但每次都返回同一个对象的指针
}
int* p = new int(42); //初始化为42
//will allocate an unnamed new object of type int,
//initialize the object to 42, and cause p to point to that object.
++*p; // p is now 43
delete p;
T* p = new T[n]; //n==0时候 new返回一个有效无意义指针off-the-end pointer
vector<T> v(p, p + n); //p合法 仍然可以这样使用
delete[] p;
char* duplicate_chars(const char* p)
{
// allocate enough space; remember to add one for the null
size_t length = strlen(p) + 1; //分配足够的空间 strlen不包含最后的空字符
char* result = new char[length];
// copy into our newly allocated space and return pointer to first element
copy(p, p + length, result);
return result;
}