代码之家  ›  专栏  ›  技术社区  ›  Meraj al Maksud SebastianX

std::string在c++中是否仅与“std::cin”一起使用[重复]

  •  -2
  • Meraj al Maksud SebastianX  · 技术社区  · 7 年前

    我最近了解到 std::substr() 通过谷歌搜索。我看到一个类似这样的代码:

    std::string s = "This is an example string";
    std::string s1 = s.substr(11, 7);
    std::cout << s1 << std::endl;
    

    现在如果我尝试使用 scanf() 函数(而不是使用 std::cin ),程序在运行时崩溃。不 std::string 支持使用 scanf() 作用

    1 回复  |  直到 5 年前
        1
  •  4
  •   Component 10    7 年前

    scanf() 属于C函数族,它是C语言而不是C++的一部分,不直接支持 std::string 并使用以null结尾的字符串。

    如果您使用的是C++,您通常应该更喜欢 标准::字符串 超过以null结尾的字符串和 the input/output library 结束 printf() / scanf() 库函数。