SlideShare a Scribd company logo
‫معرفی‬String
‫معرفی‬I/O Stream‫ها‬
01. string compose(const string& name, const string& domain)
02. {
03. return name + '@' + domain;
04. }
05.
06. auto addr = compose("dmr","bell–labs.com");
01. void m2(string& s1, string& s2)
02. {
03. s1 = s1 + 'n'; // append newline
04. s2 += 'n'; // append newline
05. }
01. string name = "Niels Stroustrup";
02.
03. void m3()
04. {
05. string s = name.substr(6,10); // s = "Stroustrup"
06. // name becomes "nicholas Stroustrup"
07. name.replace(0,5,"nicholas");
08. // name becomes "Nicholas Stroustrup"
09. name[0] = toupper(name[0]);
10. }
01. string incantation;
02.
03. void respond(const string& answer)
04. {
05. if (answer == incantation) {
06. // perform magic
07. }
08. else if (answer == "yes") {
09. // ...
10. }
11. // ...
12. }
01. void f()
02. {
03. cout << 10;
04. }
01. void g()
02. {
03. int i {10};
04. cout << i;
05. }
01. void h(int i)
02. {
03. cout << "the value of i is ";
04. cout << i;
05. cout << 'n';
06. }
the value of i is 10
01. void h2(int i)
02. {
03. cout << "the value of i is " << i << 'n';
04. }
01. void f()
02. {
03. int i;
04. cin >> i; // read an integer into i
05. double d;
06. // read a double-precision floating-point number into d
07. cin >> d;
08. }
01. void hello()
02. {
03. cout << "Please enter your namen";
04. string str;
05. cin >> str;
06. cout << "Hello, " << str << "!n";
07. }
01. void hello_line()
02. {
03. cout << "Please enter your namen";
04. string str;
05. getline(cin,str);
06. cout << "Hello, " << str << "!n";
07. }
01. struct Entry {
02. string name;
03. int number;
04. };
01. ostream& operator<<(ostream& os, const Entry& e)
02. {
03. return os << "{"" << e.name << "", " << e.number << "}";
04. }
01. // read { "name" , number } pair. Note: formatted with { " " , and }
02. istream& operator>>(istream& is, Entry& e)
03. {
04. char c, c2;
05. if (is>>c && c=='{' && is>>c2 && c2=="") { // start with a { "
06. // the default value of a string is the empty string: ""
07. string name;
08. while (is.get(c) && c!='"') // anything before a " is part of the name
09. name+=c;
10. if (is>>c && c==',') {
11. int number = 0;
12. if (is>>number>>c && c=='}') { // read the number and a }
13. e = {name,number}; // assign to the entry
14. return is;
15. }
16. }
17. }
18. is.setf(ios_base::failbit); // register the failure in the stream
19. return is;
20. }
{ "John Marwood Cleese" , 123456 }
{"Michael Edward Palin",987654}
01. for (Entry ee; cin>>ee; ) // read from cin into ee
02. cout << ee << 'n'; // write ee to cout
{"John Marwood Cleese", 123456}
{"Michael Edward Palin", 987654}
13. string, io streams

More Related Content

More from Vahid Heidari

10. copy and move
10. copy and move10. copy and move
10. copy and move
Vahid Heidari
 
9. class hierarchies
9. class hierarchies9. class hierarchies
9. class hierarchies
Vahid Heidari
 
8. abstract types
8. abstract types8. abstract types
8. abstract types
Vahid Heidari
 
7. abstraction mechanisms, containers
7. abstraction mechanisms, containers7. abstraction mechanisms, containers
7. abstraction mechanisms, containers
Vahid Heidari
 
6. separation, namespace, error
6. separation, namespace, error6. separation, namespace, error
6. separation, namespace, error
Vahid Heidari
 
5. struct, class, enum
5. struct, class, enum5. struct, class, enum
5. struct, class, enum
Vahid Heidari
 
4. pointers, arrays
4. pointers, arrays4. pointers, arrays
4. pointers, arrays
Vahid Heidari
 
3. tests, loops
3. tests, loops3. tests, loops
3. tests, loops
Vahid Heidari
 
2. types, vars, arith, consts
2. types, vars, arith, consts2. types, vars, arith, consts
2. types, vars, arith, consts
Vahid Heidari
 
1. preface, hello world
1. preface, hello world1. preface, hello world
1. preface, hello world
Vahid Heidari
 

More from Vahid Heidari (10)

10. copy and move
10. copy and move10. copy and move
10. copy and move
 
9. class hierarchies
9. class hierarchies9. class hierarchies
9. class hierarchies
 
8. abstract types
8. abstract types8. abstract types
8. abstract types
 
7. abstraction mechanisms, containers
7. abstraction mechanisms, containers7. abstraction mechanisms, containers
7. abstraction mechanisms, containers
 
6. separation, namespace, error
6. separation, namespace, error6. separation, namespace, error
6. separation, namespace, error
 
5. struct, class, enum
5. struct, class, enum5. struct, class, enum
5. struct, class, enum
 
4. pointers, arrays
4. pointers, arrays4. pointers, arrays
4. pointers, arrays
 
3. tests, loops
3. tests, loops3. tests, loops
3. tests, loops
 
2. types, vars, arith, consts
2. types, vars, arith, consts2. types, vars, arith, consts
2. types, vars, arith, consts
 
1. preface, hello world
1. preface, hello world1. preface, hello world
1. preface, hello world
 

Recently uploaded

DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
Gerardo Pardo-Castellote
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Envertis Software Solutions
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Undress Baby
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 

Recently uploaded (20)

DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 

13. string, io streams

  • 1.
  • 3. 01. string compose(const string& name, const string& domain) 02. { 03. return name + '@' + domain; 04. } 05. 06. auto addr = compose("dmr","bell–labs.com"); 01. void m2(string& s1, string& s2) 02. { 03. s1 = s1 + 'n'; // append newline 04. s2 += 'n'; // append newline 05. }
  • 4. 01. string name = "Niels Stroustrup"; 02. 03. void m3() 04. { 05. string s = name.substr(6,10); // s = "Stroustrup" 06. // name becomes "nicholas Stroustrup" 07. name.replace(0,5,"nicholas"); 08. // name becomes "Nicholas Stroustrup" 09. name[0] = toupper(name[0]); 10. }
  • 5. 01. string incantation; 02. 03. void respond(const string& answer) 04. { 05. if (answer == incantation) { 06. // perform magic 07. } 08. else if (answer == "yes") { 09. // ... 10. } 11. // ... 12. }
  • 6. 01. void f() 02. { 03. cout << 10; 04. } 01. void g() 02. { 03. int i {10}; 04. cout << i; 05. }
  • 7. 01. void h(int i) 02. { 03. cout << "the value of i is "; 04. cout << i; 05. cout << 'n'; 06. } the value of i is 10 01. void h2(int i) 02. { 03. cout << "the value of i is " << i << 'n'; 04. }
  • 8. 01. void f() 02. { 03. int i; 04. cin >> i; // read an integer into i 05. double d; 06. // read a double-precision floating-point number into d 07. cin >> d; 08. } 01. void hello() 02. { 03. cout << "Please enter your namen"; 04. string str; 05. cin >> str; 06. cout << "Hello, " << str << "!n"; 07. }
  • 9. 01. void hello_line() 02. { 03. cout << "Please enter your namen"; 04. string str; 05. getline(cin,str); 06. cout << "Hello, " << str << "!n"; 07. }
  • 10. 01. struct Entry { 02. string name; 03. int number; 04. }; 01. ostream& operator<<(ostream& os, const Entry& e) 02. { 03. return os << "{"" << e.name << "", " << e.number << "}"; 04. }
  • 11. 01. // read { "name" , number } pair. Note: formatted with { " " , and } 02. istream& operator>>(istream& is, Entry& e) 03. { 04. char c, c2; 05. if (is>>c && c=='{' && is>>c2 && c2=="") { // start with a { " 06. // the default value of a string is the empty string: "" 07. string name; 08. while (is.get(c) && c!='"') // anything before a " is part of the name 09. name+=c; 10. if (is>>c && c==',') { 11. int number = 0; 12. if (is>>number>>c && c=='}') { // read the number and a } 13. e = {name,number}; // assign to the entry 14. return is; 15. } 16. } 17. } 18. is.setf(ios_base::failbit); // register the failure in the stream 19. return is; 20. }
  • 12. { "John Marwood Cleese" , 123456 } {"Michael Edward Palin",987654} 01. for (Entry ee; cin>>ee; ) // read from cin into ee 02. cout << ee << 'n'; // write ee to cout {"John Marwood Cleese", 123456} {"Michael Edward Palin", 987654}

Editor's Notes

  1. در این ویدیو ادامه ی بحث کتابخونه استاتدارد رو پی می گیریم و در مورد string و stream ها صحبت می کنم.
  2. کتابخونه استاندارد برای کار با رشته ها یه type به نام string رو فراهم کرده. String یه سری عملیات پرکاربرد و مفید برای کار با رشته ها داره. مثلا برای concat کردن رشته ها خیلی راحت می شه از + استفاده کرد. بعد از فراخوانی تابع compose مقدار Addr برابر dmr@bell-labs.com خواهد شد. اپراتور + برای انواع مختلفی از ورودی ها تعریف شده مثلا می تونید 2 تا string رو با هم جمع کرد یا یه string با یه c-style string و یا کاراکتر با string رو جمع کرد و بهم concat کرد. همین طور دارای move constructor هم هست و اجازه می ده که حتی stringهای بزرگ رو by-value از تابع return کرد بدون اینکه کپی صورت بگیره. در بیشتر برنامه ها ما نیاز داریم که به انتهای یه string یه چیزی رو concat کنیم. برای این کار اپراتور += برای string نوشته شده و به راحتی می تونیم ازش استفاده کنیم. خط 3 و خط 4 باهم معادل هستند یعنی هم می تونید از + برای concat کردن استفاده کنید و هم از += استفاده کنید. اما دومی بهتره چون صرحتر داره مفهوم اضافه شدن یه کاراکتر رو به انتهای رشته نشون میده و efficient تره.
  3. Stringها mutable هستند یعنی اینکه میتونیم مقدار اون رو تغییر بدیم. همینطور علاوه بر اپراتور های =، +=؛ اپراتور های subscript و توابع مفیدی برای کار با زیر رشته ها و substringها داره و قابلیت کار با زیر رشته ها و دستکاری کردن اونا رو بما میده. تابع substr یه کپی از زیر رشته ای که می خوایم رو بر می گردونه. اولین آرگمان indexیی هست که می خوایم از اونجا زیر رشته شروع بشه. و دومین آرگمان طول زیر رشته هست و Index هم از صفر شروع میشه پس زیر رشته ای که توسط این خط برگردونده میشه stroustrup خواهد بود. تابع replace یه زیر رشته رو با یه زیر رشته ی دیگه جایگزین می کنه. و مشابه substr اولین آرگمان index شروع زیر رشته، آرگمان دوم طول زیر رشته و سومین آرگمان رشته ای که می خوایم جایگزین کنیم هست. با اجرای این خط niels با nicholas جایگزین میشه. نکته ای که هست اینه که برای replace کردن یه زیر رشته با یه زیر رشته ی دیگه لازم نیست که size اونا با هم برابر باشه و string برای ما عملیات جا باز کردن و یا کوچیک کردن رو انجام میده. در آخر هم اولین کاراکتر رشته رو با استفاده از تابع toupper به حرف بزرگ تبدیل کردیم.
  4. ما میتونیم stringها رو با هم مقایسه کنیم. همین طور یه string رو با یه رشته ی ثابت یا Literal هم می تونیم مقایسه کنیم. می تونیم 2 تا string رو با هم مقایسه کنیم که آیا با هم برابر هستند یا نه؟ و اینجا هم string رو با یه literal مقایسه کردیم. توی C++ به رشته ی Yes میگیم لیترال. در مورد Literalها در فصل 36 کتاب کامل بحث شده.
  5. کتابخونه استاندارد ورودی و خروجی های فرمت بندی شده رو با iostream برای ما فراهم کرده. قبلا کارکردن با اون رو دیده بودیم و از cout و cin استفاده کرده بودیم. اما بقیه ورودی خروجی ها مثل کار با کارت گرافیک و یا GUI جزء استاندارد ISO نیستند و باید از کتابخونه های دیگه استفاده کرد. در کتابخونه Iostream با استفاده از Cout ما می تونسم تمام built-in typeها رو روی خروجی چاپ کنیم. اپراتور << که اولین ورودیش ostream باشه به عنوان put to شناخته میشه و برای خروجی دادن استفاده میشه. مثلا برای اینکه عدد 10 رو خروجی بدیم به این صورت عمل می کنیم. عدد 10 تبدیل میشه به 2تا کاراکتر 1 و 0 و روی صفحه چاپ میشن. مثال بالا با مثال پایین برابر هستند در هر 2 مثال یه عدد integer رو به خروجی فرستادیم. در اولی یه عدد ثابت و در دومی یه متغییر رو روی خروجی چاپ کردیم.
  6. قبلا دیده بودیم که می تونستیم typeهای مختلفی رو با هم ترکیب کنیم و به خروجی بفرستیم مثلا می تونیم یه string رو به خروجی بفرستیم. بعد یه integer رو و بعد یه کاراکتر رو به خروجی بفرستیم. خروجی این کد به این صورت خواهد شد. ولی خیلی زود از اینکه همش cout رو تکرار کنیم خسته میشیم پس می یایم کل تابع h رو توی یک خط می نویسیم و خروجی هایی که به هم مرتبط هستند رو در کنار هم قرار میدیم. تابع h2 دقیقا همون خروجیی رو میسازه که تابع h میسازه.
  7. کتابخونه استاندارد برای ورودی istream رو پیشنهاد میده. مشابه ostream، istream هم روی built-in typeها تعریف شده و میتونه اونا رو بخونه و مقدار اونا رو برگردونه. اپراتور >> شیفت به راست برای خوندن از ورودی تعریف شده که به عنوان get from استفاده میشه. ما از Cin استفاده کرده بودیم و از ورودی مقادیر رو خونده بودیم. عملوند سمت راست >> مشخص میکنه که از ورودی چه چیزی رو می خوایم بخونیم. این خط یه عدد صحیح رو از ورودی می خونه مثلا 1234 و توی i ذخیره می کنه. و بعد یه عدد ممیز شناور رو می خونه و در d ذخیره می کنه. بعضی وقتا ما می خوایی یه سری از کاراکتر ها رو بخونیم و اونا رو بریزیم توی string . برای اینکار کافیه چیزی که جلوی cin منویسیم typeش stringباشه. Cin از ورودی یه string رو میخونه و بر می گردونه. به صورت پیش فرض withespace مثل space یا tab باعث میشن که خوندن کاراکتر ها تموم بشه و به عنوان string برگردونده بشه.
  8. برای اینکه بتونیم رشته هایی رو از ورودی بخونیم که وسطشون space داشته باشن باید از getline() استفاده کنیم. تابع getline یک خط از ورودی رو میخونه و برمیگردونه و اگه وسط خط space باشه اونا رو هم میخونه تا اینکه به کاراکتر new line یا enter برسه. آخرین کاراکتر که \n هست توسط cin حذف میشه و بقیه خط از ابتدا تا کاراکتر قبل از \n برگردونده میشه. String این ویژگی رو داره که ورودی شما هر چقدر باشه به همون اندازه حافظه میگیره و ورودی رو در خودش ذخیره میکنه و نیازی نیست که محاسبه ای برای ماکزیموم size ورودی انجام بدیم.
  9. تا اینجا ما برای خوندن و چاپ کردن از built-in typeها استفاده کردیم. علاوه بر built-in typeها می تونیم typeهای تعریف شده توسط خودمون رو هم از ورودی بخونیم و یا در خروجی چاپ کنیم. یه مثال ساده برای این کار با هم خواهیم دید. فرض کنید که می خوایم یه دفترچه تلفن ساده بسازیم .یه struct به نام entry می نویسیم که نام و شماره رو ذخیره کنه. برای اینکه بتونیم entry رو در خروجی چاپ کنیم باید یه اپراتور شیفت به چپ یا put to بنویسیم و داخل اون هر فرمتی که تمایل داریم خروجی بر اساس اون باشه رو تولید کنیم. این اپراتور ورودی اولش یه reference به یه ostream هست و ورودی دومش یه const reference به Entry هست و در بدنش هم میاد name و number رو چاپ میکنه و اول و آخر اون هم یه آکولاد باز و یه آکولاد بسته می ذاره.
  10. اما نوشتن اپراتور ورودی برای entry یکم پیچیده تره چون باید چک کنیم که وردی کاربر با فرمتی که انتظار داریم یکسان باشه و خطاهایی که ممکنه در هنگام ورود اطلاعات رخداده باشه رو هم در نظر بگیریم. خروجی اپراتور باید همون refence از istream که در ورودی دریافت میکنه باشه. از همین is میتوینم برای چک کردن اینکه خطایی رخ داده یا نه استفاده کنیم. اگه خطایی رخ نداده باشه فقط اونو بر می گردونیم. در غیر این صورت می تونیم flag یی رو set کنیم که بگیم یه خطا اتفاق افتاده. وقتی توی شرط if از is >> c استفاده می کنیم به این معنیه که آیا موفق شدیم که یه کاراکتر رو بخونیم و مقدار اونو در c ذخیره کنیم یا نه؟ این دستور یه کاراکتر رو می خونه و به صورت پیش فرض از روی کاراکتر های whitespace میپره. تابع is.get(c) هم یه کاراکتر از ورودی می خونه و مقدار اون رو در c ذخیره می کنه اما بر خلاف get from به صورت پیش فرض از روی whitespaceها نمیپره و اونا رو هم بر میگردونه. ما در این حلقه نیاز داریم که whitespaceها رو هم ذخیره کنیم چون ممکنه داخل اسمی که می خوایم از ورودی دریافت کنیم space وجود داشته باشه. ما با get from می تونیم از روی spaceهایی که خارج از اسم هستند بپریم ولی با تابع get؛ spaceهایی که داخل اسم هستند رو ذخیره کنیم.
  11. حالا می تونیم از اون اپراتور های put to و get from که نوشتیم استفاده کنیم. فرض کنید که ورودی ما به این صورت باشه. خط اول ورودی یه space بعد از آکولاد باز هست و یه tab هم قبل از آکولاد بسته هست و حظ دوم ورودی هم هیچ spaceیی نداریم. می تونیم توی main از for برای خوندن هر تعداد ورودی از نوع entry استفاده کنیم و ورودی رو که می خونیم دوباره روی خروجی چاپ کنیم. از cin میخونیم و با cout در خروجی چاپ می کنیم. خروجی این حلقه به این صورت خواهد شد.
  12. در این ویدیو string و io streamها رو معرفی کردم. و پر کار برد ترین امکانات هر کدوم رو توضیح دادم.