C++ STL库使用注意点

STL使用

在linux环境下写了一个C++程序,里面用到了STL的库函数,编译时报了很多错误。

/usr/include/c++/5/bits/stl_list.h:507:11: note:   ‘std::__cxx11::list’
main.cpp:35:10: error: ‘string’ was not declared in this scope
     list<string> strList;
          ^
main.cpp:35:10: note: suggested alternatives:
In file included from /usr/include/c++/5/iosfwd:39:0,
                 from /usr/include/c++/5/ios:38,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from main.cpp:1:
/usr/include/c++/5/bits/stringfwd.h:74:33: note:   ‘std::__cxx11::stringtypedef basic_string<char>    string;   
                                 ^
/usr/include/c++/5/bits/stringfwd.h:74:33: note:   ‘std::__cxx11::string’
main.cpp:35:18: error: ‘strList’ was not declared in this scope
     list<string> strList;
                  ^
main.cpp:43:22: error: ‘::iterator’ has not been declared
     for (list<string>::iterator iterStrList = strList.begin(); iterStrList != strList.end(); iterStrList++)
                      ^
main.cpp:43:22: note: suggested alternatives:
In file included from /usr/include/c++/5/bits/stl_algobase.h:65:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from main.cpp:1:
/usr/include/c++/5/bits/stl_iterator_base_types.h:118:12: note:   ‘std::iterator’
     struct iterator
            ^
/usr/include/c++/5/bits/stl_iterator_base_types.h:118:12: note:   ‘std::iterator’

由于所有标准模板库均在std域名空间内,所以上面一堆错误都是因为没有指定标准域名空间导致,所以千万不要忘加“using namespace std;”

你可能感兴趣的:(C/C++)