Thinking in C++ 2nd edition Volume 2 Standard Libraries & Advanced Topics revision 1 phần 5

Nếu bạn muốn tìm số lượng cho một từ cụ thể, bạn có thể sử dụng toán tử chỉ số mảng, như thế này: cout | If you want to find the count for a particular word you can use the array index operator like this I cout the wordmap the .val endl You can see that one of the great advantages of the map is the clarity of the syntax an associative array makes intuitive sense to the reader note however that if the isn t already in the wordmap a new entry will be created . A command-line argument tool A problem that often comes up in programming is the management of program arguments that you can specify on the command line. Usually you d like to have a set of defaults that can be changed via the command line. The following tool expects the command line arguments to be in the form flag1 value1 with no spaces around the so it will be treated as a single argument . The ProgVal class simply inherits from map string string C04 Program values can be changed by command line ifndef PROGVALS_H define PROGVALS_H include map include iostream include string class ProgVals public std map std string std string public ProgVals std string defaults 2 int sz void parse int argc char argv std string usage int offset 1 void print std ostream out std cout . . endif PROGVALS_H The constructor expects an array of string pairs as you ll see this allows you to initialize it with an array of char and the size of that array. The parse member function is handed the command-line arguments along with a usage string to print if the command line is given incorrectly and the offset which tells it which command-line argument to start with so you can have non-flag arguments at the beginning of the command line . Finally print displays the values. Here is the implementation C04 o include using namespace std ProgVals ProgVals Chapter 15 Multiple Inheritance 241 std string defaults 2 int sz for int i 0 i sz i insert make_pair defaults i 0 defaults i 1 void ProgVals parse int argc char argv string usage int offset Parse and apply additional command-line arguments for int i offset i .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.