当前位置:   article > 正文

c++头文件_#include

#include

头文件是c++自己定义库函数的地方,虽然它定义的函数的方式我们看不懂,但是像int,char,memset等函数都是他在头文件里给你编好的,下边给大家看一下我们熟悉的iostream头文件里面是什么样的:

  1. // Standard iostream objects -*- C++ -*-
  2. // Copyright (C) 1997-2014 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/iostream
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // ISO C++ 14882: 27.3 Standard iostream objects
  25. //
  26. #ifndef _GLIBCXX_IOSTREAM
  27. #define _GLIBCXX_IOSTREAM 1
  28. #pragma GCC system_header
  29. #include <bits/c++config.h>
  30. #include <ostream>
  31. #include <istream>
  32. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /**
  36. * @name Standard Stream Objects
  37. *
  38. * The &lt;iostream&gt; header declares the eight <em>standard stream
  39. * objects</em>. For other declarations, see
  40. * http://gcc.gnu.org/onlinedocs/libstdc++/manual/io.html
  41. * and the @link iosfwd I/O forward declarations @endlink
  42. *
  43. * They are required by default to cooperate with the global C
  44. * library's @c FILE streams, and to be available during program
  45. * startup and termination. For more information, see the section of the
  46. * manual linked to above.
  47. */
  48. //@{
  49. extern istream cin; /// Linked to standard input
  50. extern ostream cout; /// Linked to standard output
  51. extern ostream cerr; /// Linked to standard error (unbuffered)
  52. extern ostream clog; /// Linked to standard error (buffered)
  53. #ifdef _GLIBCXX_USE_WCHAR_T
  54. extern wistream wcin; /// Linked to standard input
  55. extern wostream wcout; /// Linked to standard output
  56. extern wostream wcerr; /// Linked to standard error (unbuffered)
  57. extern wostream wclog; /// Linked to standard error (buffered)
  58. #endif
  59. //@}
  60. // For construction of filebuffers for cout, cin, cerr, clog et. al.
  61. static ios_base::Init __ioinit;
  62. _GLIBCXX_END_NAMESPACE_VERSION
  63. } // namespace
  64. #endif /* _GLIBCXX_IOSTREAM */

iostream库里面就定义了我们所熟知的cin和cout函数

 其他的还有cerr,clog,wcin,wcout,wcerr,wclog等等函数。而有些人就不太明白了,那头文件还能导入头文件吗?

 

答案是肯定的,因为它里面的一些函数如static,extern,namespace等函数也需要引入头文件,因为里面定义函数的过程过于深奥,所以不在这里过多讲述。

下面再给大家啃一个不同的头文件内部:

  1. // Components for manipulating sequences of characters -*- C++ -*-
  2. // Copyright (C) 1997-2014 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/string
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // ISO C++ 14882: 21 Strings library
  25. //
  26. #ifndef _GLIBCXX_STRING
  27. #define _GLIBCXX_STRING 1
  28. #pragma GCC system_header
  29. #include <bits/c++config.h>
  30. #include <bits/stringfwd.h>
  31. #include <bits/char_traits.h> // NB: In turn includes stl_algobase.h
  32. #include <bits/allocator.h>
  33. #include <bits/cpp_type_traits.h>
  34. #include <bits/localefwd.h> // For operators >>, <<, and getline.
  35. #include <bits/ostream_insert.h>
  36. #include <bits/stl_iterator_base_types.h>
  37. #include <bits/stl_iterator_base_funcs.h>
  38. #include <bits/stl_iterator.h>
  39. #include <bits/stl_function.h> // For less
  40. #include <ext/numeric_traits.h>
  41. #include <bits/stl_algobase.h>
  42. #include <bits/range_access.h>
  43. #include <bits/basic_string.h>
  44. #include <bits/basic_string.tcc>
  45. #endif /* _GLIBCXX_STRING */

这个头文件就是属于很多人也都知道的<string>头文件,看到这个头文件,你们有没有发现什么?对了,就是这个头文件string基本都是由其他的头文件组成的,什么<bits/range_access.h>等等的头文件。引入头文件就相当于把这个头文件拥有了其他头文件的能力,这个能力指的就是其他头文件定义的函数,数据类型等。我们的程序要导入头文件也是同一个原因。

那头文件有没有基本上都是程序的呢?也有。再来看下一个:

  1. // Helpers for ostream inserters -*- C++ -*-
  2. // Copyright (C) 2007-2014 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/ostream_insert.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{ostream}
  23. */
  24. #ifndef _OSTREAM_INSERT_H
  25. #define _OSTREAM_INSERT_H 1
  26. #pragma GCC system_header
  27. #include <iosfwd>
  28. #include <bits/cxxabi_forced.h>
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  32. template<typename _CharT, typename _Traits>
  33. inline void
  34. __ostream_write(basic_ostream<_CharT, _Traits>& __out,
  35. const _CharT* __s, streamsize __n)
  36. {
  37. typedef basic_ostream<_CharT, _Traits> __ostream_type;
  38. typedef typename __ostream_type::ios_base __ios_base;
  39. const streamsize __put = __out.rdbuf()->sputn(__s, __n);
  40. if (__put != __n)
  41. __out.setstate(__ios_base::badbit);
  42. }
  43. template<typename _CharT, typename _Traits>
  44. inline void
  45. __ostream_fill(basic_ostream<_CharT, _Traits>& __out, streamsize __n)
  46. {
  47. typedef basic_ostream<_CharT, _Traits> __ostream_type;
  48. typedef typename __ostream_type::ios_base __ios_base;
  49. const _CharT __c = __out.fill();
  50. for (; __n > 0; --__n)
  51. {
  52. const typename _Traits::int_type __put = __out.rdbuf()->sputc(__c);
  53. if (_Traits::eq_int_type(__put, _Traits::eof()))
  54. {
  55. __out.setstate(__ios_base::badbit);
  56. break;
  57. }
  58. }
  59. }
  60. template<typename _CharT, typename _Traits>
  61. basic_ostream<_CharT, _Traits>&
  62. __ostream_insert(basic_ostream<_CharT, _Traits>& __out,
  63. const _CharT* __s, streamsize __n)
  64. {
  65. typedef basic_ostream<_CharT, _Traits> __ostream_type;
  66. typedef typename __ostream_type::ios_base __ios_base;
  67. typename __ostream_type::sentry __cerb(__out);
  68. if (__cerb)
  69. {
  70. __try
  71. {
  72. const streamsize __w = __out.width();
  73. if (__w > __n)
  74. {
  75. const bool __left = ((__out.flags()
  76. & __ios_base::adjustfield)
  77. == __ios_base::left);
  78. if (!__left)
  79. __ostream_fill(__out, __w - __n);
  80. if (__out.good())
  81. __ostream_write(__out, __s, __n);
  82. if (__left && __out.good())
  83. __ostream_fill(__out, __w - __n);
  84. }
  85. else
  86. __ostream_write(__out, __s, __n);
  87. __out.width(0);
  88. }
  89. __catch(__cxxabiv1::__forced_unwind&)
  90. {
  91. __out._M_setstate(__ios_base::badbit);
  92. __throw_exception_again;
  93. }
  94. __catch(...)
  95. { __out._M_setstate(__ios_base::badbit); }
  96. }
  97. return __out;
  98. }
  99. // Inhibit implicit instantiations for required instantiations,
  100. // which are defined via explicit instantiations elsewhere.
  101. #if _GLIBCXX_EXTERN_TEMPLATE
  102. extern template ostream& __ostream_insert(ostream&, const char*, streamsize);
  103. #ifdef _GLIBCXX_USE_WCHAR_T
  104. extern template wostream& __ostream_insert(wostream&, const wchar_t*,
  105. streamsize);
  106. #endif
  107. #endif
  108. _GLIBCXX_END_NAMESPACE_VERSION
  109. } // namespace std
  110. #endif /* _OSTREAM_INSERT_H */

这个是一个不太知名的头文件<ostream_insert.h>头文件,他基本全是由代码组成,也就是定义的函数和数据类型较多,导入的头文件较少。

接下来就到了一个重头戏:如何导入头文件?

这个问题可能有些人看完前面的就已经有答案了,就是#include+<导入头文件名称>就行了。

十分简单。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/65260
推荐阅读
相关标签
  

闽ICP备14008679号