CからC++への遷移の概要
7468 ワード
CからC++言語への転換
1.普段使いはprintf、scanfがメイン
printfとscanfはcin coutに比べて効率が速い
2.ヘッダファイルを以下のように変更し、C++はC言語に互換性がある
3.アルゴリズムではdefineではなくconst変数の使用を推奨
4.stringクラスはchar[]より文字列を処理するのが便利ですが、stringはcin cout入力出力しか使用できません.
cinの読み込みはスペースで区切り、行全体を読み込むにはgetline(cin,s)が必要です.
サブストリングを切り取るためにsubstrを使用できます
5.C++書写構造体はキーワードstructを書く必要がない
6.C++での参照
7.vectorの使用
8.set集合
setの集合内容はそれぞれ異なり、set内の要素は小さくて大きい順に並べ替えられます
9.map
10.stack
11.queue
12.unordered_mapとunordered_set
13.bitset
14.cmp
sortデフォルトは⼩から大配列までであり、3番目のパラメータcmp関数を指定してもよい.次に、1つのcmp関数を定義して並べ替え規則~cmpを指定するのは構造体の中で最もよく用いられるのか、特に多くの並べ替えの問題項目~⽐は1つの学的構造体stuのように学号と成績の2つの変数があり、成績が異なる場合は成績の大きさから⼩まで並べ替えることが要求される.成績が同じで学号に従って⼩から大まで並べば、cmp配列を1つ書いて複雑に見える並べ替え過程を実現することができます.
15.cctypeisalnumアルファベット番号 isalphaアルファベット isblank空白かどうかをチェック iscntrl制御文字 isdigit 10進数 islower小文字tolowerは小文字 isprint印刷可能かどうか ispunct句読点 isspace Space isupper大文字toupperから大文字 へisxdigit 16進数 16.C++11 auto
autoはC++11⾥12207;の新しい特性であり,コンパイラが初期値タイプから変数のタイプを直接推定できる.
Dev-Cappstring、 unordered_map、unordered_set、autoこれらは、devがc++11~を支持するように設定する必要がある~プロセスコンパイルオプション-コンパイラ-コンパイル時にこのコマンド「-std=c++11」を加えればよい
STLで反復器を使用する場合、autoは1つの列の反復器タイプ宣言に代わることができます.
17.to_string
18.stoi, stod
1.普段使いはprintf、scanfがメイン
printfとscanfはcin coutに比べて効率が速い
2.ヘッダファイルを以下のように変更し、C++はC言語に互換性がある
#include // C #include
#include // C #include
#include // C #include
#include // C #include
3.アルゴリズムではdefineではなくconst変数の使用を推奨
const int i=123456789;
4.stringクラスはchar[]より文字列を処理するのが便利ですが、stringはcin cout入力出力しか使用できません.
string s = "hello world"; //
string s2 = s;
string s3 = s + s2; // +
string s4;
len = s.length();
cin >> s4; //
cout << s; //
cinの読み込みはスペースで区切り、行全体を読み込むにはgetline(cin,s)が必要です.
サブストリングを切り取るためにsubstrを使用できます
string s2 = s.substr(4); // 4
string s3 = s.substr(5, 3); // 5 ,3
5.C++書写構造体はキーワードstructを書く必要がない
struct stu {
int grade;
float score;
};
struct stu arr1[10]; // C struct
stu arr2[10];// C++
6.C++での参照
void func(int &a) { // n , n , func
a
a = 99;
}
int main() {
int n = 0;
func(n); // n 0 99
}
7.vectorの使用
#include
#include
using namespace std;
int main() {
vector a; // vector
cout << a.size() << endl; // size 0
for (int i = 0; i < 10; i++) {
a.push_back(i); // vector a i
}
cout << a.size() << endl; // a size 10
vector b(15); // vector , b 0
cout << b.size() << endl;
for (int i = 0; i < b.size(); i++) {
b[i] = 15;
}
vector c(20, 2); // vector
for (int i = 0; i < c.size(); i++) {
cout << c[i] << " ";
}
cout << endl;
for (auto it = c.begin(); it != c.end(); it++) { //
vector
cout << *it << " ";
}
return 0;
}
8.set集合
setの集合内容はそれぞれ異なり、set内の要素は小さくて大きい順に並べ替えられます
#include
#include
using namespace std;
int main() {
set s; // s
s.insert(1); // s 1
cout << *(s.begin()) << endl; // s (
)
for (int i = 0; i < 6; i++) {
s.insert(i); // s i
}
for (auto it = s.begin(); it != s.end(); it++) { // s
cout << *it << " ";
}
cout << endl << (s.find(2) != s.end()) << endl; // s , s.end() ( s.end() s )
cout << (s.find(10) != s.end()) << endl; // s.find(10) != s.end()
10
s.erase(1); // s 1
cout << (s.find(1) != s.end()) << endl; // 1 ~
return 0;
}
9.map
#include
#include
10.stack
#include
#include
using namespace std;
int main() {
stack s; // s
for (int i = 0; i < 6; i++) {
s.push(i); // i s
}
cout << s.top() << endl; // s
cout << s.size() << endl; // s
s.pop(); //
return 0;
}
11.queue
#include
#include
using namespace std;
int main() {
queue q; // q
for (int i = 0; i < 6; i++) {
q.push(i); // i q
}
cout << q.front() << " " << q.back() << endl; //
cout << q.size() << endl; //
q.pop(); //
return 0;
}
12.unordered_mapとunordered_set
unordered_map
ヘッダ#include
中、unordered_set
ヘッダ#include
中~unordered_mapとmap(またはunordered_setとset)の違いは、mapがキー値ペアのキー順にソート(set(またはunordered_set)コード運用時間を短縮し、コード効率を向上させる~適用法とmap、setは同一である13.bitset
#include
#include
#include
using namespace std;
bool cmp(int a, int b) { // cmp bool
return a > b; //
}
int main() {
vector v(10);
for (int i = 0; i < 10; i++) {
cin >> v[i];
}
sort(v.begin(), v.end());// cmp, ,v
int arr[10];
for (int i = 0; i < 10; i++) {
cin >> arr[i];
}
sort(arr, arr + 10, cmp); // arr , cmp
return 0;
}
14.cmp
#include
using namespace std;
struct stu { // stu,number ,score
int number;
int score;
}
bool cmp(stu a, stu b) { // cmp , bool,
stu
if (a.score != b.score) // ,
return a.score > b.score;
else // ,
return a.number < b.number;
}
// if-else C ~
bool cmp(stu a, stu b) {
return a.score != b.score ? a.score > b.score : a.number <
b.number;
}
sortデフォルトは⼩から大配列までであり、3番目のパラメータcmp関数を指定してもよい.次に、1つのcmp関数を定義して並べ替え規則~cmpを指定するのは構造体の中で最もよく用いられるのか、特に多くの並べ替えの問題項目~⽐は1つの学的構造体stuのように学号と成績の2つの変数があり、成績が異なる場合は成績の大きさから⼩まで並べ替えることが要求される.成績が同じで学号に従って⼩から大まで並べば、cmp配列を1つ書いて複雑に見える並べ替え過程を実現することができます.
15.cctype
#include
#include
using namespace std;
int main() {
char c;
cin >> c;
if (isalpha(c)) {
cout << "c is alpha";
}
return 0;
}
autoはC++11⾥12207;の新しい特性であり,コンパイラが初期値タイプから変数のタイプを直接推定できる.
Dev-Cappstring、 unordered_map、unordered_set、autoこれらは、devがc++11~を支持するように設定する必要がある~プロセスコンパイルオプション-コンパイラ-コンパイル時にこのコマンド「-std=c++11」を加えればよい
STLで反復器を使用する場合、autoは1つの列の反復器タイプ宣言に代わることができます.
// set :
for(set::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << " ";
}
// :
for(auto it = s.begin(); it != s.end(); it++) {
cout << *it << " ";
}
17.to_string
18.stoi, stod