ผลต่างระหว่างรุ่นของ "Ske algo lab/read input"
ไปยังการนำทาง
ไปยังการค้นหา
Jittat (คุย | มีส่วนร่วม) (หน้าที่ถูกสร้างด้วย ': ''This is part of Ske algo lab.'' == Detecting EOF for cin == This is the template for 3n+1. == Detecting EOF for getline == ...') |
Jittat (คุย | มีส่วนร่วม) |
||
(ไม่แสดง 5 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน) | |||
แถว 5: | แถว 5: | ||
This is the template for 3n+1. | This is the template for 3n+1. | ||
+ | <syntaxhighlight lang="cpp"> | ||
+ | #include <iostream> | ||
+ | #include <string> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int i, j; | ||
+ | while(cin >> i >> j) { | ||
+ | // ... | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
== Detecting EOF for getline == | == Detecting EOF for getline == | ||
− | You can use <tt>std::getline</tt> to read the whole line. The following is the template for TEX quotes. | + | You can use <tt>std::getline</tt> to read the whole line. The following is the template for TEX quotes. You can access string length with <tt>length</tt> method and you can access each character using []. See [http://www.cplusplus.com/reference/string/string/ reference here]. |
+ | |||
+ | <syntaxhighlight lang="cpp"> | ||
+ | #include <iostream> | ||
+ | #include <string> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | string st; | ||
+ | while(getline(cin, st)) { | ||
+ | // ... | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Fast IO == | ||
+ | |||
+ | In C++, <tt>std::cin</tt> and <tt>std::cout</tt> can be very slow. You can add the following two lines before you start using both statements to improve speed. | ||
+ | |||
+ | <syntaxhighlight lang="cpp"> | ||
+ | ios::sync_with_stdio(false); | ||
+ | cin.tie(NULL); | ||
+ | </syntaxhighlight> |
รุ่นแก้ไขปัจจุบันเมื่อ 20:47, 30 สิงหาคม 2567
- This is part of Ske algo lab.
Detecting EOF for cin
This is the template for 3n+1.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i, j;
while(cin >> i >> j) {
// ...
}
}
Detecting EOF for getline
You can use std::getline to read the whole line. The following is the template for TEX quotes. You can access string length with length method and you can access each character using []. See reference here.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string st;
while(getline(cin, st)) {
// ...
}
}
Fast IO
In C++, std::cin and std::cout can be very slow. You can add the following two lines before you start using both statements to improve speed.
ios::sync_with_stdio(false);
cin.tie(NULL);