C 語言常用小點
C字符串
C 基礎-指針魏宽,函數(shù)處理器
C 文件操作
JNI 基礎 C語言版
C++ 基礎知識點大綱
[C++ 基礎經(jīng)驗知識點]
C++ 基礎代碼模板和使用
C++ 基礎-定點模式
C++ 宏定義
C++ 指針區(qū)分
C++ 指針特別篇-指針轉換和智能指針
C++ 類的繼承和多繼承
C++ this 原理
C++淺拷貝和深拷貝的原理
C++ 函數(shù)
C++ 仿函數(shù)
C++ 友元函數(shù)理解
C++ STL
C++ 模板函數(shù)純虛函數(shù)和Java對比
C++ 函數(shù)運算符重載(二)化簡版
C++ 多線程
C++ 算法包和源碼簡析
C++ 引用
引用變量奔穿,作為別名嗡贺,共享存儲
引用就想夫妻倆,共用一個內存 ,形參影響實參。
引用基本使用
#include <iostream>
#include <string>
using namespace std;
int main()
{
int rats = 101;
int &rodents = rats;
cout << "rats=" << rats << endl;
cout << "rodens=" << rodents << endl;
rodents++;
cout << "rats=" << rats << endl;
cout << "rodens=" << rodents << endl;
cout << "&rats=" << &rats << endl;
cout << "&rodens=" << &rodents << endl;
int bunnies = 50;
rodents = bunnies;
cout << "bunnies = " << bunnies;
cout << ",rats = " << rats;
cout << ",rodents = " << rodents << endl;
cout << "bunnies addr = " << &bunnies;
cout << ",rats addr = " << &rats;
cout << ",rodents addr = " << &rodents << endl;
return 0;
}
引用傳慘,是否可以修改,const可以限制參數(shù)修改
#include <iostream>
#include <string>
using namespace std;
void swapr(int & a, int & b);
void swapr2(int * a, int * b);
void swapr3(int a, int b);
int main()
{
int wallet1 = 100;
int wallet2 = 350;
cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;
cout << "using references to swap contents:\n";
swapr(wallet1,wallet2);
cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;
cout << "using pointers to swap contents:\n";
swapr2(&wallet1,&wallet2);
cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;
cout << "using value to swap contents:\n";
swapr3(wallet1,wallet2);
cout << "wallet1 = "<< wallet1 << " wallet2 = " << wallet2 << endl;
cout << &"abc" << endl;
return 0;
}
void swapr(int & a, int & b)
{
int temp = a;
a = b;
b = temp;
}
void swapr2(int * a, int * b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void swapr3(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
結構體的引用
#include <iostream>
#include <string>
struct free_throws
{
std::string name;
int made;
int attempts;
float percent;
};
void display(const free_throws & ft);
void set_pc(free_throws & ft);
free_throws & accumulate(free_throws &target, const free_throws &source);
int main()
{
free_throws one = {"Ifelsa Branch", 13, 14};
free_throws two = {"Andor Knott", 10, 16};
free_throws three = {"Minnie Max", 7, 9};
free_throws four = {"Whily Looper", 5, 9};
free_throws five = {"Long Long", 6, 14};
free_throws team = {"Throwgoods", 0, 0};
free_throws dup;
set_pc(one);
display(one);
accumulate(team, one);
display(team);
// use return value as argument
display(accumulate(team, two));
accumulate(accumulate(team, three), four);
display(team);
// use return value in assignment
dup = accumulate(team,five);
std::cout << "Displaying team:\n";
display(team);
std::cout << "Displaying dup after assignment:\n";
display(dup);
set_pc(four);
// ill-advised assignment
accumulate(dup,five) = four;
std::cout << "Displaying dup after ill-advised assignment:\n";
display(dup);
// std::cin.get();
return 0;
}
void display(const free_throws & ft)
{
using std::cout;
cout << "Name: " << ft.name << '\n';
cout << " Made: " << ft.made << '\t';
cout << "Attempts: " << ft.attempts << '\t';
cout << "Percent: " << ft.percent << '\n';
}
void set_pc(free_throws & ft)
{
if (ft.attempts != 0)
ft.percent = 100.0f *float(ft.made)/float(ft.attempts);
else
ft.percent = 0;
}
free_throws & accumulate(free_throws & target, const free_throws & source)
{
target.attempts += source.attempts;
target.made += source.made;
set_pc(target);
return target;
}
引用和類壹粟,如果是char * 類型字符串和 string 參數(shù)不匹配,我們需要在前面加上const
最后一個函數(shù)的引用不存在,所以打印不到內容趁仙。
include <iostream>
#include <string>
using namespace std;
string version1(const string &s1, const string &s2);
const string &version2(string &s1, const string &s2);
const string &version3(string &s1, const string &s2);
int main()
{
string input;
string copy;
string result;
cout << "Enter a string: ";
getline(cin, input);
cout << "Your string as entered: " << input << endl;
result = version1(input, "***");
cout << "Your change string: " << result << endl;
cout << "Your original string: " << input << endl;
cout << "-----------------------" << endl;
result = version2(input, "***");
cout << "Your change string: " << result << endl;
cout << "Your original string: " << input << endl;
cout << "-----------------------" << endl;
cout << "Reset original stirng: \n";
input = copy;
result = version3(input,"@@@");
cout << "Your change string: " << result << endl;
cout << "Your original string: " << input << endl;
return 0;
}
string version1(const string &s1, const string &s2)
{
// return s2 + s1 + s2;
string temp;
temp = s2 + s1 + s2;
return temp;
}
// const string &s2 類型不匹配洪添,必須加入const,前面右值“***”付給左值s2
// 返回值 const string &
const string &version2(string &s1, const string &s2)
{
s1 = s2 + s1 + s2;
return s1;
}
// 這個是錯誤的temp被釋放雀费,引用已經(jīng)不存在
const string &version3(string &s1, const string &s2)
{
string temp;
temp = s2 + s1 + s2;
return temp;
}
ostream 引用等
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int LIMIT = 5;
void file_it(ostream &os, double fo, double *fe, int n);
int main()
{
ofstream fout;
const char *fn = "ep-data.txt";
fout.open(fn);
if (!fout.is_open())
{
cout << "can not open " << fn << "Bye ." << endl;
exit(EXIT_FAILURE);
}
double objective;
cout << "Enter the focal length of your tlescope objective in mm: ";
cin >> objective;
double eps[LIMIT];
cout << "Enter the focal length of eyeleces in mm:\n";
for (int i = 0; i < LIMIT; i++)
{
cout << "Eyepiece # " << i + 1 << ": ";
cin >> eps[i];
}
file_it(cout, objective, eps, LIMIT);
file_it(fout, objective, eps, LIMIT);
return 0;
}
void file_it(ostream &os, double fo, double *fe, int n)
{
os << "Focal length of objective: " << fo << " mm\n";
os << "f.l. eyepiece\t"
<< "magnification" << endl;
for (int i = 0; i < n; i++)
{
os << fe[i];
os << int(fo / fe[i] + 0.5) << endl;
}
}