Accelerated C++ 10.1

指针、数组、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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值