اولین بار است که به این وب‌سایت می‌آیید. راهنما را مطالعه کنید!
x
موسسه خیریه محک

شهر ریاضی بازی-ریاضی

خواندن ورودی

+1 امتیاز
233 نمایش

چه دستوراتی برای خوندن از ورودی بطور کلی هست؟

یک مشکل در مورد C++ این است که هم‌اکنون گرایشی قوی در زبان وجود دارد که شما را وادار می‌کند پیش از انجام هر کاری، همه‌ی جوانب آن را بدانید. (Larry Wall)
پرسیده شده جمعه ۱۷ آذر ۱۳۹۱ در C++ توسط CPP_Crawler2  
بازتگ شده شنبه ۲۴ فروردین ۱۳۹۲ توسط admin

منظورت چه کدهایی هستن؟

1 پاسخ

0 امتیاز

#include <cstdio>
#include <iostream>

template <typename Test>


void test (Test t)
{
const clockt begin = clock();
t();
const clock
t end = clock();
std::cout << (end-begin)/double(CLOCKSPERSEC) << " sec\n";
} void stdio() {
std::string line;
unsigned dependency
var = 0; while (!feof (stdin)) { int c; line.clear(); while (EOF != (c = fgetc(stdin)) && c!='\n') line.push_back (c); dependency_var += line.size(); } std::cout << dependency_var << '\n';

}

void synced() {
std::iosbase::syncwithstdio (true);
std::string line;
unsigned dependency
var = 0;
while (getline (std::cin, line)) {
dependencyvar += line.size();
}
std::cout << dependency
var << '\n';
}

void unsynced() {
std::iosbase::syncwithstdio (false);
std::string line;
unsigned dependency
var = 0;
while (getline (std::cin, line)) {
dependencyvar += line.size();
}
std::cout << dependency
var << '\n';
}

void usage() { std::cout << "one of (synced|unsynced|stdio), pls\n"; }

int main (int argc, char *argv[]) {
if (argc < 2) { usage(); return 1; }

if (std::string(argv[1]) == "synced") test (synced);
else if (std::string(argv[1]) == "unsynced") test (unsynced);
else if (std::string(argv[1]) == "stdio") test (std_io);
else { usage(); return 1; }

return 0;

}

این کد رو تو ubuntu کامپایل کن، نتیجه این میشه:

cat testfile | ./a.out stdio
...
0.34 sec

cat testfile | ./a.out synced
...
1.31 sec

cat testfile | ./a.out unsynced
...
0.08 sec

و این کد:

#include <cstdio>
#include <iostream>

template <typename Test>


void test (Test t)
{
const clockt begin = clock();
t();
const clock
t end = clock();
std::cout << (end-begin)/double(CLOCKSPERSEC) << " sec\n";
} void scanf() {
char x,y,c;
unsigned dependency
var = 0; while (!feof (stdin)) { scanf ("%c%c%c", &x, &y, &c); dependency_var += x + y + c; } std::cout << dependency_var << '\n';

}

void unsynced() {
std::iosbase::syncwithstdio (false);
char x,y,c;
unsigned dependency
var = 0;
while (std::cin) {
std::cin >> x >> y >> c;
dependencyvar += x + y + c;
}
std::cout << dependency
var << '\n';
}

void usage() { std::cout << "one of (scanf|unsynced), pls\n"; }

int main (int argc, char *argv[]) {
if (argc < 2) { usage(); return 1; }

if (std::string(argv[1]) == "scanf") test (scanf_);
else if (std::string(argv[1]) == "unsynced") test (unsynced);
else { usage(); return 1; }

return 0;

}

که نتیجه اینجوریه:

scanf: 0.63 sec
unsynced stream: 0.41

به نظر میاد که کار با scanf خیلی جالب نیست!

همون طور که از زمانهای اندازه گیری شده برای یک فایل متنی بزرگ مشخصه اینه که کار باStream(cin & cout) خیلی سریعتر از scanf هستش...

Don't Expect Anyone Except Yourself,...
پاسخ داده شده پنجشنبه ۸ فروردین ۱۳۹۲ توسط Arash