Chương trình 1:Tại sao tôi không thể xây dựng một hàng đợi/ngăn xếp với các danh sách khởi tạo kèm cặp ngoặc nhọn? (C++ 11)
#include <iostream>
#include <cstdlib>
#include <vector>
int main(){
//compiles successfully
std::vector<int> vec{1,2,3,4,5};
return EXIT_SUCCESS;
}
Chương trình 2:
#include <iostream>
#include <cstdlib>
#include <queue>
int main(){
//compiler error
std::queue<int> que{1,2,3,4,5};
return EXIT_SUCCESS;
}
Thông báo lỗi:
main.cpp: In function ‘int main()’:
main.cpp:7:31: error: no matching function for call to ‘std::queue<int>::queue(<brace-enclosed initializer list>)’
main.cpp:7:31: note: candidates are:
/usr/include/c++/4.6/bits/stl_queue.h:141:7: note: std::queue<_Tp, _Sequence>::queue(_Sequence&&) [with _Tp = int, _Sequence = std::deque<int, std::allocator<int> >]
/usr/include/c++/4.6/bits/stl_queue.h:141:7: note: candidate expects 1 argument, 5 provided
/usr/include/c++/4.6/bits/stl_queue.h:137:7: note: std::queue<_Tp, _Sequence>::queue(const _Sequence&) [with _Tp = int, _Sequence = std::deque<int, std::allocator<int> >]
/usr/include/c++/4.6/bits/stl_queue.h:137:7: note: candidate expects 1 argument, 5 provided
/usr/include/c++/4.6/bits/stl_queue.h:92:11: note: std::queue<int>::queue(const std::queue<int>&)
/usr/include/c++/4.6/bits/stl_queue.h:92:11: note: candidate expects 1 argument, 5 provided
/usr/include/c++/4.6/bits/stl_queue.h:92:11: note: std::queue<int>::queue(std::queue<int>&&)
/usr/include/c++/4.6/bits/stl_queue.h:92:11: note: candidate expects 1 argument, 5 provided
Câu hỏi:
tại sao hàng đợi không thể được khởi chạy như vectơ?
Tôi cho rằng chúng không phải là vùng chứa chuỗi, nhưng tại sao điều đó lại quan trọng?
Tôi chắc chắn có lý do chính đáng, nhưng tôi không thể tìm thấy bất kỳ giải thích nào.
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Bạn có thể kiểm tra tham chiếu về vùng chứa [tại đây] (http://www.cplusplus.com/reference/stl/). Bạn cũng có thể xem xét boost :: assign library [here] (http://www.boost.org/doc/libs/1_42_0/libs/assign/doc/index.html). –