代码之家  ›  专栏  ›  技术社区  ›  Maxpm

如何包含字符串头?

  •  26
  • Maxpm  · 技术社区  · 15 年前

    我想了解 string s、 但是不同的消息来源告诉我要包含不同的标题。

    <string.h> ,但其他人提到 "apstring.h" apstring ,但我听说另一个更强大。当我包括 <字符串。h> 尝试声明一些字符串变量,但是,我得到了错误。正确的用法是什么?

    8 回复  |  直到 14 年前
        1
  •  67
  •   Community Mohan Dere    9 年前

    你想包括 <string> 使用 std::string

    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string s = "a string";
        std::cout << s << std::endl;
    }
    

    但你真正需要做的是 introductory level book . 你不会以任何其他方式正确地学习,当然也不会为了网上的信息而放弃。

        2
  •  13
  •   Rob Kennedy    15 年前

    Advanced Placement 计算机科学课程。它描述了 你将在整个课程中使用的字符串类,一些考试问题可能会涉及到它,并希望你对它有一定的了解。除非你是那个班的学生或是为了参加那个考试而学习,否则不要理会那些消息来源。

    告诉你使用String .h的消息来源不是真的谈论C++,或者严重过时。你也应该忽略它们。那个标题是给 C类 用于操作以空结尾的字符数组(也称为C样式字符串)的函数。

    #include <string> 在你文件的顶部。当声明变量时,类型为 string ,它在 std 命名空间,因此其全名为 std::string . 通过遵循大量介绍性文本的示例并说 using namespace std 不

        3
  •  6
  •   yubaolee    15 年前

    我没听说过“apstring”。如果你想在c++中使用string,你可以这样做:

    #include<string>
    using namespace std;
    int main()
    {
       string str;
       cin>>str;
       cout<<str;
       ...
       return 0;
    }
    

        4
  •  4
  •   Community Mohan Dere    9 年前

    C++字符串类是 std::string . 要使用它,您需要包括 <string>

    关于如何使用 ,您需要咨询 good introductory C++ book .

        5
  •  3
  •   paxdiablo    15 年前

    string.h 如果你在C++中编码。C++中的字符串是 std::string 比旧的C风格的“字符串”更容易使用的变体。使用:

    #include <string>
    

    std::string s 宣布一个。你能用的很多奇妙的方法 标准::字符串 here .

    如果您查看了有关使用C字符串的堆栈溢出的大量问题,您将了解为什么应该尽可能避免它们:-)

        6
  •  1
  •   Cardinal System    6 年前

    "apstring" 不是标准C++,在C++中,你想 #include 这个 <string>

        7
  •  0
  •   Conner devopensource    14 年前

    使用这个:

    #include < string>
    
        8
  •  0
  •   Kimbluey    11 年前

    也许这个链接会帮助你。

    std::string documentation .

    #include <string>

    推荐文章