每天讀一點點C++primer!半年讀完就可以!
7/4
- 標準庫定義了4個l0對象。爲了處理輸入,我們使用一個名爲cin(發音爲see-in)的istream類型的對象。這個對象也被稱爲標準輸入(standardinput)。對於輸出,我們使用一個名爲cout(發音爲see-out)的ostream類型的對象。此對象也被稱爲標準輸出(standard output)。標準庫還定義了其他兩個ostream對象,名爲cerr和clog(發音分別爲see-err和see-log)。我們通常用cerr來輸出警告和錯誤消息,因此它也被稱爲標準錯誤(standard error)。而clog用來輸出程序運行時的一般性信息。
- IO對象無拷貝或者賦值
- 在函數參數括號寫字符串被定義成const char *,最近經常碰到
- 局部靜態對象只在第一次經過時初始化
- 當形參有頂層const時,傳給它常量對象和非常量對象都是可以的
- 打算明天開始寫項目多用指針和引用了,當時學算法不知道聽了誰的鬼話儘量不用指針,結果現在還不熟。還是輪子哥說的靠譜。
8.1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
istream& task(istream &a) {
string m;
while (a >> m) {
cout << m << endl;
}
a.clear();
return a;
}
int main()
{
std::filebuf in;
if (!in.open("E:/GitCode/Messy_Test/testdata/istream.data", std::ios::in)) {
std::cout << "fail to open file" << std::endl;
return;
}
istream wss(&in);
task(wss);
}
istream是所有輸入類型的基類,直接cin調用即可。
6.2.1
#include "stdio.h"
void change(int* a, int* b) {
int c = *a;
*a = *b;
*b = c;
}
int main() {
int a, b;
a = 1;
b = 2;
int* c = &a;
int* d = &b;
change(c, d);
printf("a=%d b=%d",a,b);
}
6.2.2
#include "stdio.h"
void change(int& a, int& b) {
int c = a;
a = b;
b = c;
}
int main() {
int a, b;
a = 1;
b = 2;
change(a, b);
printf("a=%d b=%d", a, b);
}
6.2.3
#include "stdio.h"
#include <string>
std::string::size_type find_char(std::string& const s,char c,std::string::size_type &occurs) {
auto ret = s.size();
occurs = 0;
for (decltype(ret)i = 0; i != s.size(); i++) {
if (s[i] == c) {
if (ret == s.size()) {
ret = i;
}
++occurs;
}
}
return ret;
}
int main() {
std::string s = "sdafodsago";
std::string::size_type ctr;
auto index = find_char(s, 'o', ctr);
}
問題:我不明白一個課後問題,爲什麼std::string::size_type find_char(std::string& const s,char c,std::string::size_type &occurs)這個函數三個參數必須分別是現在的格式?或者說第一個參數用引用比不用引用好在哪裏?(必須被定義爲const的前提下)
明天打算重讀引用那一章找出答案。
7/5
- 昨天問題的答案是:引用避免了新建對象,提高了速度
- 引用本身不是對象,不能定義引用的引用
- 如果你想要一個string形參能夠接受常量,那麼使用const;事實上,任何無需在函數中改變的字符串都應該被定義爲常量引用
int *matrix[10]; //十個指針構成的數組
int (*matrix)[10]; //指向含有十個整數的數組的指針
- const cast用於改變const屬性
- 一旦函數的某個形參被賦予了默認值,它後面的參數必須都有默認值
- 內斂函數可以在調用點內聯展開,適用於優化流程直接規模小調用頻繁的函數。
- constexpr函數是指能用於常量表達式的函數,它被隱式地指定爲內聯函數
- constexpr函數是指能用於常量表達式的函數:函數的返回值類型和所有形參的類型必須是“字面值類型”:算術、引用、指針。並且函數體內有且只有一條return語句。
6.2.4
#include "stdio.h"
#include<iostream>
#include<string>
using namespace std;
int function(int a,int *b,int *c) {
std::cout<< *c++;
std::cout << *c++;
return 0;
}
void error_msg(initializer_list<string> il) {
for (auto beg = il.begin(); beg != il.end(); beg++) {
cout << *beg;
}
}
void add(initializer_list<int> il) {
int sum = 0;
for (auto beg = il.begin(); beg != il.end(); beg++) {
sum += *beg;
}
cout << sum;
}
int main(int argc, char **argv) {
int a = 1,b = 2;
int *c = &b;
int j[2] = { 0,1 };
//function(a, c,j);
argv[0];
//std::cout << argv[1] << argv[2];
}
6.3.2
#include "stdio.h"
#include<iostream>
#include<string>
using namespace std;
char &get_val(string &str, string::size_type ix) {
return str[ix];
}
int main() {
string s("a value");
cout << s << endl;
get_val(s, 0) = 'A';
cout << s << endl;
return 0;
}
#include "stdio.h"
#include<iostream>
#include<string>
#include<vector>
#include <cstdlib>
using namespace std;
char &get_val(string &str,string::size_type ix) {
return str[ix];
}
int mainx() {
return 100000;
}
vector<string> process() {
return { "i","am","sb" };
}
int (*func(int i))[10]{
return 0;
}
//尾置返回類型
auto funcc(int i)->int(*)[10]{
}
//decltype
int odd[] = { 1,3,5,7,9 };
decltype(odd) *arrPtr(int i) {
}
string *testO(string &a) {
}
const string &shorterString(const string & s1, const string &s2) {
return s1.size() <= s2.size() ? s1 : s2;
}
string &shorterString(string &s1, string& s2) {
}
int main() {
string a[] = {"a","b","c"};
testO(&a[]);
string s("a value");
cout << s << endl;
get_val(s, 0) = 'A';
cout << s << endl;
return EXIT_FAILURE;
return EXIT_SUCCESS;
}