[心得] c++2
Standard C++ string class
Header file : #include <string>
Namespace : using namespace std;
ex. string s1,s2; //empty strings
string s3="fuck 5566?"; //初始值
Reading / writing files with fstream
Header file : #include <fstream>
ifstream in(" 檔名 "); //open for reading
ofstream out(" 檔名 "); //open for writing
ex.
#include <fstream>
#include <string>
using namespace std;
ifstream inf("inputData.txt");
string line;
vector<string> lines;
while (getline(inf, line))
lines.push_back(line);//堆疊進入動態陣列
Standard C++ vector class
Header file : #include <vector>
Namespace : using namespace std;
// 下面是一段運用 vector 容器物件的 iterator 來依序列印所有
// 元素的程式碼
vector<string>::iterator iter;
for (iter=lines.begin(); iter<lines.end(); iter++)
cout << *iter << endl;
Class
Encapsulation 封裝
|____ public (Member fuction
|____ private (Data member)
1.Private data can only be accessed in member fuctions
2.It does not mean they can only be accessed thought
object
參考(Reference)
請看下面 C 程式中指標變數運用的範例
#include <cstdio>
void twice(int *xaddr)
{
*xaddr = *xaddr * 2;
}
...
void main()
{
int x = 10;
twice(&x);
printf("twice of 10 is %d\n", x);
}
在 C++ 中如果運用參考變數語法的話,要達到相同功能的程式如下:
#include <cstdio>
void twice(int &y)
{
y = y * 2;
}
void main()
{
int x = 10;
twice(x);
printf("twice of 10 is %d\n", x);
}
inline 是告訴編譯器在翻譯這個程式中呼叫這個函式的敘述,例如 r = square(x+y);
,時將這個敘述換成類似 z = x + y; r = z * z; 這樣子等效的敘述,以增進程式執行
時的效率,注意 inline 函式的宣告只對於同一個檔案內呼叫 square 的敘述有作用。
inline double square(double x)
{
return x * x;
}
Declare Variables
C:Local varuiables must be declared at beginning of a block
C++:Local variables can be declared anywhere in a block,the scope extends to
the end of the block
ex. void main()
{
int array[5]={1,2,3,4};
cout < array[0]<<endl;
...
...
int sum=0;
for(int=0;i<5;i++)
{
sum+=array[i];
cout<<sum;
}
1.better readability 2. encourages single-variables
Constant
char string1[10];
char string2[10];
string1[0]='t';
string2[0]='t';
const char *ptrstring1=string1;
ptrstring1[0]='T' ; //illegal
ptrstring1 ++; //leagl
const *char ptrstring2=string1;
ptrstring1[0]='T' ; //legal
ptrstring1 ++; //illeagl
Macros
容易錯 int x=5,y=6;
cout << square(x+y); ~~>Call macros (x+y*x+y)=(5+6*5+6)
ans is not (5+6)^2 = 121
Corrections
#define square(x) ((x)*(x)) p.s. (x)=(x+y)
Static (靜態)
如果函式作用域內能夠有某種變數,其生命週期在函式結束後依然持續
舉例來說,我在一個class中定義 static test()
那麼,這一個東西,不管如何他是不同時會去被修改到的東西
我也可以在main主函式中
直接呼叫 class test();來做執行的動作
不用像是一般呼叫函式一樣,要先定義然後才能去做呼叫
因為我們在 利用 static時 已經先初始化了
用static宣告的變數
在記憶體的位址就只有一個
不管你產生了多少物件
而不是像其他變數,隨著物件宣告也跟著產生
然而靜態函式只能處裡靜態變數
allocate/deallocate memory
____________________________________________
| C | malloc | free |
|__C++__|____new______|_delete , delete[]____|
C: array = (int*)malloc(sizeof(int)*100); <=====> free array;
C++: array = new int [100]; <=====> delete [] array;array=0;
--
▄▄▄▄▄
▄▄ ▄▄ ▄
▄▄▄▄▄▄▄▄▄▄▄
▄▄▄▄▄▄ ▄▄▄▄▄▄
▄▄▌██▌█▌▌ ▌▌▄▌
▄▌█▄▄▄▄█▄▄▄▄▄▌█▄
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.81.45
推
04/15 01:24, , 1F
04/15 01:24, 1F
→
04/15 01:25, , 2F
04/15 01:25, 2F
推
04/15 01:25, , 3F
04/15 01:25, 3F
推
04/15 01:28, , 4F
04/15 01:28, 4F