Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
soft:c [2020/07/25 08:45] Ce Zhangsoft:c [2020/11/05 02:10] (current) Ce Zhang
Line 1: Line 1:
 =====C++===== =====C++=====
-====Read & write files====+ 
 +==== 2D array ==== 
 +<code> 
 +TGraph*** g = new TGraph**[Nfile]; 
 +for(i loop) g[i] = new TGraph*[t[i]->Nentries]; 
 +for(int j = 0; j<t[i]->Nentries;j++) g[i][j] = ...(TGraph *)... 
 +</code> 
 + 
 +====EOF==== 
 +<code> 
 +<<EOF        //开始 
 +.... 
 +EOF            //结束 
 + 
 +//将一个文件的内容输出到另一个文件中: 
 +# cat fileA > fileB 
 +//将"<< EOF EOF"替代输入对象文件fileA: 
 +# cat << EOF > fileB 
 +//命令执行后,提示用户输入内容,输入结束后,用户的输入内容被保存到了fileB中。 
 +</code> 
 + 
 +====I/O====
  
 <code> <code>
Line 32: Line 53:
 </code> </code>
  
-====将变量名替换为字符串==== 
  
 +<code>
 +std::vector<char> v;
 +
 + if (FILE *fp = fopen("filename", "r"))
 + {
 + char buf[1024];
 + while (size_t len = fread(buf, 1, sizeof(buf), fp))
 + v.insert(v.end(), buf, buf + len);
 + fclose(fp);
 + }
 +</code>
 +====将变量名替换为字符串====
 +<code>
 #define name2str(name) (#name) #define name2str(name) (#name)
 +</code>
 +
 +<code>    
 +#define xstr(s) str(s)
 +#define str(s) #s
 +#define foo 4
 +
 +     str (foo)
 +          ==> "foo"
 +     xstr (foo)
 +          ==> xstr (4)
 +          ==> str (4)
 +          ==> "4"
 + </code>
 +
 +====函数指针作为参数====
 +<code>
 +
 +void DiffusionModel (bool (*GeometryFunction)(double, double, double)) )
 +bool InsideAerogel(double x, double y, double z);
 +
 +DiffusionModel(
 +    InsideAerogel
 +);
 +</code>
 +
 +====char * 转换 (for input arg.)====
 +<code>
 +//char* to int
 +
 +int a = stoi(argv[1]);
 + 
 +//char* 转 float/double
 +float b = stof(argv[2]);
 +double bb = stod(argv[2]);
 + 
 +//char* 转 string (可以直接转)
 +string str = argv[3];
 +cout << a << endl << b << endl << bb << endl << str << endl;
 +</code>