2014-03-26 28 views
8

我試圖讓我的輸出看起來像這樣:如何使用iomanip的setw,setfill和左/右? Setfill沒有停止其輸出

size  time1  time2 
------------------------------- 
10   4   8 
100  48   16 
1000  2937  922 
10000  123011  3902 
100000  22407380 830722 

而且我知道我需要使用運輸及工務局局長,setfill,離開了。但是,我的嘗試不斷給我不正確的輸出。這裏是我的代碼一個例子:

std::cout << "size" << std::setw(20) << "time" << std::setw(20) << "time2\n"; 
std::cout << std::setfill('-') << std::setw(60) << "-" << std::endl; 
run = 10; 
for(int i = 0; i < 5; i++) { 
    std::cout << run; 
    run *= 10; 
    std::cout << std::setw(20) << std::left << time1[i]; 
    std::cout << std::setw(20) << std::left << time2[i] << "\n"; 
} 

而這裏的輸出:

size time1 time2 
------------------------------------------------------------ 
103-------------------13------------------ 
100171-----------------199----------------- 
100013183---------------667----------------- 
10000670130--------------8205---------------- 
10000014030798-------------1403079888--------- 

我試着改變,我使用運輸及工務局局長的命令,setfill,離開了,但我現在只是盲目飛行。我也搜索了iomanip教程。我遵循指示 - 據我所知 - 但我仍然沒有得到它。

如何阻止運行過的setfill?我如何證明左邊是正確的,但用setw來阻止這些數字互相碰撞?

回答

3

如何:

std::cout << "size" << std::setw(20) << "time" << std::setw(20) << "time2\n"; 
std::cout << std::setfill('-') << std::setw(60) << "-" << std::endl; 
run = 10; 
std::cout << std::setfill(' '); //fill with spaces 
for(int i = 0; i < 5; i++) { 
    std::cout << std::setw(20) << std::left << run; // fill the run column 
    run *= 10; 
    std::cout << std::setw(20) << std::left << time1[i]; 
    std::cout << std::setw(20) << std::left << time2[i] << "\n"; 
}