cpp2.dvi

Similar documents
(STL) STL 1 (deta structure) (algorithm) (deta structure) 2 STL STL (Standard Template Library) 2.1 STL STL ( ) vector<int> x; for(int i = 0; i < 10;

cpp1.dvi

cpp4.dvi

1.3 ( ) ( ) C

exec.dvi

1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >>

double 2 std::cin, std::cout 1.2 C fopen() fclose() C++ std::fstream 1-3 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.t

Condition DAQ condition condition 2 3 XML key value

void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >=

2017_08_ICN研究会_印刷用

6-1


Qt (Generic Containers) Java STL <QtAlgorithms> STL (Generic Algorithms) QList<T>, QLinkedList<T>, QVector<T>, QStack<T>, QQueue<T> QMap<Key,

programmingII2019-v01

,,,,., C Java,,.,,.,., ,,.,, i

ohp03.dvi

untitled


design_pattern.key

program.dvi

r03.dvi

解きながら学ぶC++入門編

C言語によるアルゴリズムとデータ構造

r07.dvi

C のコード例 (Z80 と同機能 ) int main(void) { int i,sum=0; for (i=1; i<=10; i++) sum=sum + i; printf ("sum=%d n",sum); 2

ohp07.dvi

橡ボーダーライン.PDF

( ) ( ) 30 ( ) 27 [1] p LIFO(last in first out, ) (push) (pup) 1

@okuraofvegetabl

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

解きながら学ぶC++入門編

r08.dvi

Parametric Polymorphism

新版 明解C++入門編

Green with White Lines

連載講座 : 高生産並列言語を使いこなす (5) 分子動力学シミュレーション 田浦健次朗 東京大学大学院情報理工学系研究科, 情報基盤センター 目次 1 問題の定義 17 2 逐次プログラム 分子 ( 粒子 ) セル 系の状態 ステップ 18

‚æ4›ñ

新版明解C言語 実践編

untitled

基礎プログラミング2015

BW BW


C¥×¥í¥°¥é¥ß¥ó¥° ÆþÌç

2) TA Hercules CAA 5 [6], [7] CAA BOSS [8] 2. C II C. ( 1 ) C. ( 2 ). ( 3 ) 100. ( 4 ) () HTML NFS Hercules ( )

2006 [3] Scratch Squeak PEN [4] PenFlowchart 2 3 PenFlowchart 4 PenFlowchart PEN xdncl PEN [5] PEN xdncl DNCL 1 1 [6] 1 PEN Fig. 1 The PEN


PowerPoint Presentation

ohp08.dvi

tkk0408nari

yacc.dvi

(search: ) [1] ( ) 2 (linear search) (sequential search) 1

text_08.dvi

untitled


:30 12:00 I. I VI II. III. IV. a d V. VI

C 言語経験者のための C++ 入門

Boo Boo 2 Boo 2.NET Framework SDK 2 Subversion 2 2 Java 4 NAnt 5 Boo 5 5 Boo if 11 for 11 range 12 break continue 13 pass

スライド 1

GP05取説.indb

ohp11.dvi

r11.dvi

LIFO(last in first out, ) 1 FIFO(first in first out, ) 2 2 PUSH POP : 1

:30 12:00 I. I VI II. III. IV. a d V. VI

untitled

*.....J.....S.q..2013B_....


ex12.dvi

haskell.gby

C++0x

# let st1 = {name = "Taro Yamada"; id = };; val st1 : student = {name="taro Yamada"; id=123456} { 1 = 1 ;...; n = n } # let string_of_student {n

1.ppt


VE-GP32DL_DW_ZA

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

VDM-SL VDM VDM-SL Toolbox VDM++ Toolbox 1 VDM-SL VDM++ Web bool

Python C/C++ IPMU IRAF

ALG ppt

PFQX2227_ZA

tuat1.dvi

Introduction Purpose This training course demonstrates the use of the High-performance Embedded Workshop (HEW), a key tool for developing software for

C , C++ C C++ C++ C cpprefjp - C++ 1 C CUI 2.1 donothing.cpp 1

Quick Sort 計算機アルゴリズム特論 :2017 年度 只木進一

TopLink å SampleClient.java... 5 Ò readallsample() querysample() cachesample() Ç..

05_fuke.indd

untitled

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

取扱説明書_KX-PW100CL

1

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

4-3- 基 C++ に関する知識 オープンソースシステムのソースを解読する上で C++ の知識は必須であるといえる 本カリキュラムでは まずオブジェクト指向に関する Ⅰ. 概要理解を深め クラスの扱い方について学習し STL を使用してアルゴリズムとデータ構造を実装する方法を学習する Ⅱ. 対象専

RX600 & RX200シリーズ アプリケーションノート RX用仮想EEPROM

3.1 stdio.h iostream List.2 using namespace std C printf ( ) %d %f %s %d C++ cout cout List.2 Hello World! cout << float a = 1.2f; int b = 3; cout <<

pptx

プログラミング方法論 II 第 14,15 回 ( 担当 : 鈴木伸夫 ) 問題 17. x 座標と y 座標をメンバに持つ構造体 Point を作成せよ 但し座標 は double 型とする typedef struct{ (a) x; (b) y; } Point; 問題 18. 問題 17 の

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

/

取説_KX-PW101CL_PW102CW

2 ColorSpace DepthSpace CameraSpace Kinect V2 Kinect V2 BOdyIndex 3. NtKinect Kinect V2 C++ NtKinect [4] NtKinect = Kinect SDK + + STL(C++) + OpenCV +

2

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

Transcription:

2018 c 2 C++ (2) STL, C++ 21 string 22 STL 23 21 string C, \0, (, ), (, ), /,,,, C++,,, string string,,,,,, <string> include,,, int, << >> >>,,,, getline(, string ), [List 21] 2: #include <string> 3: 4: int main(void) 5: { 6: std::string message = "Enter strings"; 7: std::cout << message << ": "; 8: std::string s; 9: std::cin >> s; 10: std::cout << "word 1 = " << s << std::endl; 2 1

11: std::cin >> s; 12: std::cout << "word 2 = " << s << std::endl; 13: getline(std::cin,s); 14: std::cout << "line = " << " " << s << " " << std::endl; 15: getline(std::cin,s); 16: std::cout << "line = " << " " << s << " " << std::endl; 17: return 0; 18: } Enter strings: This is a pen, word1 = This word2 = is line = a pen 1 That is a book, line = That is a book, + += s+=a s a == <, <=, >, >= [List 22] 2: #include <string> 3: 4: int main(void) 5: { 6: std::string a = "C"; 7: std::string b = " "; 8: std::string c = "programming"; 9: 10: std::string x = a + b + c; // x = "C programming" 11: std::string y = a + "++" + b + c; // y = "C++ programming" 12: std::string z = a; // z = "C" 13: z += b; // z = "C " 14: z += c; // z = "C programming" 15: 16: std::cout << "x = " << x << std::endl; 17: std::cout << "y = " << y << std::endl; 18: std::cout << "z = " << z << std::endl; 2 2

19: 20: if (x==y) std::cout << "x==y\n"; 21: else if (x<y) std::cout << "x<y\n"; // 22: else std::cout << "x>y\n"; 23: 24: if (x==z) std::cout << "x==z\n"; // 25: else if (x<z) std::cout << "x<z\n"; 26: else std::cout << "x>z\n"; 27: 28: return 0; 29: }, x = C programming y = C++ programming z = C programming x<y x==z,, string s, 1 slength() s ( ) 2 s[i] s i ( s[0]) s = "woman" s[3]= e, s "women" 3 ssubstr(i,k) s i k s = "fukada kyoko", ssubstr(4,6) "da kyo" 2 k, i s = "fukada kyoko", ssubstr(7) "kyoko" 4 sfind(t) s t, snpos ( ), s = "to be or not to be", sfind("be")==3, sfind("and")==snpos 5 sreplace(i,k,t) s i k, t t k, s t, s = "I live in Osaka", sreplace(2,7,"love") s "I love Osaka" s "often" "usually", sreplace(sfind("often"),5,"usually") (s "often" ) 6 sinsert(i,t) s i t 21, Entry Entry, string name, string phone, 1 ("079-xxx-xxxx" "xxx-xxxx" ) 2, [List 23] 2 3

[List 23] 2: #include <string> 3: 4: class Entry { 5: public: 6: std::string name; 7: std::string phone; 8: Entry(const std::string& nm="", const std::string& ph="") { 9: name = nm; 10: phone = ph; 11: } 12: }; 13: 14: std::ostream& operator<<(std::ostream& os, const Entry& e) { 15: os << ename << ": " << ephone; 16: return os; 17: } 18: 19: int main(void) 20: { 21: Entry e[10]; 22: 23: int n = 0; 24: e[n++] = Entry("Kwansei Gakuin University (PR)", "0798-54-6017"); 25: e[n++] = Entry("Kwansei Gakuin University (KSC)", "079-565-7600"); 26: e[n++] = Entry("Kobe University", "078-881-1212"); 27: e[n++] = Entry("Sanda Woodytown SATY", "079-564-8800"); 28: e[n++] = Entry("Sanda Hotel", "079-564-1101"); 29: 30: for (int i=0; i<n; i++) { 31: // ------------------------------------------------ 32: // "079-xxx-xxxx" "xxx-xxxx" 33: // ------------------------------------------------ 34: } 35: 36: std::cout << std::endl; 37: std::cout << " : "; 38: std::string s; 39: std::cin >> s; 40: // ------------------------------------------------ 41: // name s 42: // ------------------------------------------------ 43: 44: return 0; 45: } 2 4

std::, using namespace std;, 22 STL STL Standard Template Library, C++,,, (container; ),,, int, string,,, STL,,,, 221 vector vector <vector> include T vector, vector<t>, 10, int a[10], vector, vector<int> a(10);, i a[i], size() ( ) [List 24] 2: #include <vector> 3: 4: int main(void) { 5: std::vector<int> a(5); 6: std::cout << "asize = " << asize() << std::endl; 7: // asize = 5 8: for (int i=0; i<asize(); i++) a[i] = i; 9: for (int i=0; i<asize(); i++) std::cout << a[i] << " "; 10: std::cout << std::endl; 11: // 0 1 2 3 4 12: 2 5

13: std::vector<int> b(3); 14: b[0] = 7; b[1] = 5; b[2] = 3; 15: 16: std::vector<int> c(10); 17: for (int i=0; i<csize(); i++) c[i] = i*i; 18: 19: a = b; 20: std::cout << "asize = " << asize() << std::endl; 21: // asize = 3 22: for (int i=0; i<asize(); i++) std::cout << a[i] << " "; 23: std::cout << std::endl; 24: // 7 5 3 25: 26: a = c; 27: std::cout << "asize = " << asize() << std::endl; 28: // asize = 10 29: for (int i=0; i<asize(); i++) std::cout << a[i] << " "; 30: std::cout << std::endl; 31: // 0 1 4 9 16 25 36 49 64 81 32: 33: aresize(30); 34: // asize = 30 35: std::cout << "asize = " << asize() << std::endl; 36: for (int i=0; i<asize(); i++) std::cout << a[i] << " "; 37: std::cout << std::endl; 38: // 0 1 4 9 16 25 36 49 64 81 0 0 0 39: 40: return 0; 41: } 5: <> (template) vector<int> int, vector 1 stack,, stack, ( ) 19: vector, ( ) 26: vector,,, 1), 2), 3),, vector,,, reserve(int s), s, capacity() a[i] i, 2 6

,, resize(int newsize) vector, vector<t>, T, ( ) 1 void push back(t d) d 2 T& back() 3 void pop back() [List 25] 2: #include <vector> 3: 4: main(void) { 5: 6: std::vector<int> s; 7: spush back(5); 8: spush back(7); 9: spush back(9); 10: spush back(11); // s = (5 7 9 11) 11: 12: std::cout << "size = " << ssize() << std::endl; // size = 4 13: for (int i=0; i<ssize(); i++) { 14: std::cout << s[i] << " "; 15: } 16: std::cout << std::endl; 17: 18: std::cout << sback() << std::endl; // 11 19: spop back(); // s = (5 7 9) 20: std::cout << sback() << std::endl; // 9 21: spop back(); // s = (5 7) 22: 23: std::cout << "size = " << ssize() << std::endl; // size = 2 24: for (int i=0; i<ssize(); i++) { 25: std::cout << s[i] << " "; 26: } 27: std::cout << std::endl; 28: 29: return 0; 30: } push back(t) 1,,, g++ vector vector push back(t), 2 (push back() capacity() ) 2 7

vector, vector vector, vector<vector<int> > 2 >, (>>) 22 21 [List 23], vector<entry> 24 28 push back 222 deque deque vector <deque> T deque std::deque<t>, deque,std::deque<int> d; 1 push front(t d) T d, a[0], a[0], a[1], a[2], a[1], a[2], a[3], 2 T& front() ( vector<t> ) 3 pop front() push back() pop front(), (queue) push front(t) pop front() vector<t>, n O(n) ( ), std::deque<t> O(1) ( ), (a[i]) vector<t> ( ) 223 list list [] 1 <list> include 2 T std::list<t> list,std::list<int> a; iterator ( ), std::list<t> iterator, std::list<t>::iterator, std::list<t> iterator, std::list<int>::iterator p; 2 8

, [List 26] 2: #include <list> 3: 4: int main(void) 5: { 6: std::list<int> li; 7: 8: lipush back(3); // li = (3) 9: lipush back(7); // li = (3 7) 10: lipush front(2); // li = (2 3 7) 11: lipush front(5); // li = (5 2 3 7) 12: 13: // 14: std::list<int>::iterator p; 15: for (p=libegin(); p!=liend(); p++) { 16: std::cout << *p << " "; 17: } 18: std::cout << std::endl; // 5 2 3 7 19: 20: // 21: std::list<int>::reverse iterator r; 22: for (r=lirbegin(); r!=lirend(); r++) { 23: std::cout << *r << " "; 24: } 25: std::cout << std::endl; // 7 3 2 5 26: 27: return 0; 28: } x T, p, q, r std::list<t> iterator 1 push back(x), push front(x) vector deque 2 insert(p,x) p x 1 pop back(x), pop front(x) vector deque 2 erase(p) p 3 erase(p,q) p q 4 clear() 1 front() 2 back() 3 begin() iterator 4 end() iterator () 2 9

5 rbegin() reverse iterator ( iterator) 6 rend() reverse iterator begin() end() front() back() rend() rbegin() iterator p iterator 1 *p p, m (*p)m p->m 2 p++ p 3 p-- p 4 std::list<t> li, p iterator, for (p=libegin(); p!=liend(); p++) { *p = } reverse iterator reverse terator iterator iterator r reverse terator 1 *r r 2 r++ r ( ) 3 r-- r ( ) 4 std::list<t> li, for (r=lirbegin(); r!=lirend(); r++) { *r = } r-- 23, Record, [ (id), (name), (score)], (operator<<), Seiseki data Record Seiseki operator<< 3 insert, lookup, erase worst, 1 insert(int id, const std::string& nm, int s): [ id, nm, s] 2 lookup(int id): id, std::cout ( not found ) 3 erase worst(): Record,, insert, lookup, 1,,, iterator p p++, p, p++, iterator (vector), 2 10

37 p, iterator const iterator,, const,, iterator const iterator [List 27] [List 27] 2: #include <string> 3: #include <list> 4: 5: // 1 6: class Record { 7: public: 8: int id; // 9: std::string name; // 10: int score; // 11: Record() {} 12: Record(int i, const std::string& nm, int s) { 13: id = i; 14: name = nm; 15: score = s; 16: } 17: }; 18: 19: std::ostream& operator<<(std::ostream& os, const Record& r) 20: { 21: os << "[" << rid << "] " << rname << " : " << rscore; 22: return os; 23: } 24: 25: // ( ) 26: class Seiseki { 27: public: 28: std::list<record> data; 29: void insert(int, const std::string&, int); 30: void lookup(int) const; 31: void erase worst(); 32: }; 33: 34: std::ostream& operator<<(std::ostream& os, const Seiseki& s) 35: { 36: os << "*** ***\n"; 37: for (std::list<record>::const iterator p = sdatabegin(); 38: p!= sdataend(); p++) { 39: os << *p << "\n"; 40: } 41: return os; 42: } 2 11

43: 44: void Seiseki::insert(int id, const std::string& nm, int s) 45: { 46: // ----------------------------- 47: // 48: // ----------------------------- 49: } 50: 51: void Seiseki::lookup(int id) const 52: { 53: // ----------------------------- 54: // 55: // ----------------------------- 56: } 57: 58: void Seiseki::erase worst() 59: { 60: // ----------------------------- 61: // 62: // ----------------------------- 63: } 64: 65: int main(void) 66: { 67: Seiseki s; 68: 69: // 70: sinsert(7001,"aaaa",89); 71: sinsert(7123,"bbbb",70); 72: sinsert(7013,"cccc",55); 73: sinsert(7200,"dddd",99); 74: sinsert(7087,"eeee",83); 75: 76: // 77: std::cout << s; 78: 79: // id (0 ) 80: int id; 81: std::cout << "> "; 82: std::cin >> id; 83: while (id!=0) { 84: slookup(id); 85: std::cout << "> "; 86: std::cin >> id; 87: } 88: 89: // 90: serase worst(); 91: 2 12

92: // 93: std::cout << s; 94: 95: return 0; 96: } 224 map map ( ),,, ( ), a[0], a[1],, a["ishiura"], a["akina"],, (red-black tree), n O(logn), 1 <map> include 2 ( ) K, T map std::map<k,t> string int map, std::map<std::string,int> 3 map m, k, m[k] string [List 28] 2: #include <map> 3: #include <string> 4: 5: int main(void) 6: { 7: std::map<std::string,int> semester; 8: semester["logic circuits"] = 1; 9: semester["compiler"] = 5; 10: semester["formal language and automata"] = 3; 11: 12: std::cout << semester["logic circuits"] << "\n"; 13: std::cout << semester["compiler"] << "\n"; 14: std::cout << semester["network"] << "\n"; 15: 16: return 0; 17: } 1 5 0 2 13

map list, map iterator, begin(), end(),, map, (pair), iterator first, second [List 29] 1: std::map<std::string,int>::iterator p; 2: for (p=semesterbegin(); p!=semesterend(); p++) { 3: std::cout << p->first << ": " << p->second << std::endl; 4: }, [List 28] [List 29] stringmap, "network", [],, find(k) ( K ) find(k key) key map, iterator, end() p iterator, erase(p) p semester, [List 210] 1: std::string s; 2: std::cout << ">"; 3: std::cin >> s; 4: if ((p=semesterfind(s))==semesterend()) { 5: std::cout << s << " not found" << std::endl; 6: } 7: else { 8: std::cout << "deleting " << p->first << std::endl; 9: semestererase(p); 10: } 24 21 [List 23] map map<std::string,std::string>, 23 STL,,,,,,, 2 14

STL,,, STL, C++, 231 STL iterator vector v,, i for (i=0; i<vsize(); i++){v[i] = }, list iterator, p v iterator,, for (p=vbegin(); p!=vend(); p++){*p = } iterator STL (,, iterator ( ) ) vector deque list map begin, end O(1) O(1) O(1) O(1) rbegin, rend O(1) O(1) O(1) O(1) front, back O(1) O(1) O(1) push back, pop back O(1) O(1) O(1) push front, pop front O(1) O(1) [] O(1) O(1) O(log n) insert O(n) O(n) O(1) O(log n) erase O(n) O(n) O(1) O(log n) size O(1) O(1) O(1) O(1) ==,!=, < O(n) O(n) O(n) O(n) 232 find find (, map, find ) p1, p2 iterator, d, find(p1,p2,d), p1 p2 d, iterator,, p2 2 15

[List 211] 2: #include <list> 3: #include <algorithm> 4: 5: typedef std::list<int> int list; 6: typedef int list::iterator int list iter; 7: 8: int main(void) 9: { 10: // li = (3 5 2 3 2 3) 11: int list li; 12: lipush back(3); 13: lipush back(5); 14: lipush back(2); 15: lipush back(3); 16: lipush back(2); 17: lipush back(3); 18: 19: std::cout << ">"; 20: int i; 21: std::cin >> i; 22: 23: // i 24: int list iter p = find(libegin(), liend(), i); 25: if (p==liend()) { 26: // 27: std::cout << "not found\n"; 28: } 29: else { 30: // 31: std::cout << *p << " found\n"; 32: 33: // 34: p = find(++p, liend(), i); 35: if (p==liend()) { 36: std::cout << "not found\n"; 37: } 38: else { 39: std::cout << *p << " found again\n"; 40: } 41: } 42: 43: return 0; 44: } 25 [List 211], list vector 2 16

[List 211] 233 sort sort [] ( vector, deque) list,, sort p1, p2 iterator,, sort(p1,p2), p1 p2 sort quick sort, 1 O(nlogn), O(n 2 ) 2 stable sort stable sort O(nlognlogn), O(nlogn) [List 212] 2: #include <vector> 3: #include <algorithm> 4: 5: int main(void) 6: { 7: // a = (12 25 1 9 30 4) 8: std::vector<int> a; 9: apush back(12); 10: apush back(25); 11: apush back(1); 12: apush back(9); 13: apush back(30); 14: apush back(4); 15: 16: // 17: sort(abegin(), aend()); 18: 19: // 20: for (int i=0; i<asize(); i++) { 21: std::cout << a[i] << " "; 22: } 23: std::cout << std::endl; 24: 25: return 0; 26: } sort 2 17

,, 3 [List 212], 15 15 sort(abegin(), aend(), std::greater<int>()); sort 3 22 Phonebook, [List 213] 1: class by name { // 2: public: 3: bool operator()(const Entry& e1, const Entry& e2) const { 4: return e1name < e2name; 5: } 6: }; 7: 8: class by phone { // 9: public: 10: bool operator()(const Entry& e1, const Entry& e2) const { 11: return e1phone < e2phone; 12: } 13: }; 14: 15: 16: int main(void) 17: { 18: sort(ebegin(),eend(), by name()); // 19: sort(ebegin(),eend(), by phone()); // 20: } 26 22 vector Phonebook, STL,, STL, 2 18

: auto C++11, auto, std::vector<int> a; apush back(3); apush back(5); apush back(7); for (std::vector<int>::iterator p=abegin(); p!=aend(); p++) { } std::cout << *p << std::endl; for (auto p=abegin(); p!=aend(); p++) { } std::cout << *p << std::endl;, a for (auto&& e : a) { } std::cout << e << std::endl; for (const auto& e : a) { } std::cout << e << std::endl;, auto, auto&, const auto&, auto&& g++ C++11 C++14, -std (g++ ) g++ -std=c++11 autocpp g++ -std=c++14 autocpp Nagisa ISHIURA 2 19