可能重复:
我在哪里可以找到当前的C或C++标准文档?
我在哪里可以找到完整的C++ 11标准?我知道它的特征是漂浮在互联网上,但我似乎无法找到文件本身.
在C++ 11之后,我想到了c_str()并且data() 等效.
C++ 17为后者引入了一个重载,返回一个非常量指针(引用,我不确定它是否完全在C++ 17中更新):
const CharT* data() const; (1)
CharT* data(); (2) (since C++17)
Run Code Online (Sandbox Code Playgroud)
c_str() 只会返回一个常量指针:
const CharT* c_str() const;
Run Code Online (Sandbox Code Playgroud)
为什么在C++ 17中区分这两种方法,特别是当C++ 11是使它们成为同构的时候?换句话说,为什么只有一种方法过载,而另一种方法没有?
以前这是std::string::c_str()工作,但是从C ++ 11开始,data()它也提供了工作,为什么还要c_str()添加null终止字符std::string::data()?在我看来,这似乎是在浪费CPU周期,如果null终止字符根本不相关而仅data()使用,C ++ 03编译器就不必关心终止符,也不必每次调整字符串大小时,都必须向终止符写入0,但是由于data()-null-guarantee,C ++ 11编译器不得不浪费每次写入字符串大小时都要写入0的周期,因此,这可能会使代码变慢,我想他们有一定理由要添加该保证,那是什么?
我为什么要打电话std::string::data()过来std::string::c_str()?当然,这里标准的疯狂有一些方法......
我有一个std::string.我需要将其转换std:string为Cstring.
我尝试使用.c_str()但它只用于非unicode项目,我使用unicode项目(因为非unicode项目与VS2013一起被贬低).
任何人都可以告诉我如何转换std::string为CStringunicode项目?
可能重复:
字符串c_str()与data()
我strncpy(dest, src_string, 32)用来转换std::string为char[32]使我的C++类与遗留的C代码一起使用.但是std :: string的c_str()方法总是返回一个以null结尾的字符串吗?
string::c_str()调用时实际上做了什么?
string::c_str()将分配内存,复制字符串对象的内部数据并将空终止字符附加到新分配的内存中?或者
string::c_str()分配内存并复制过来。string实际上,始终存在空终止符是唯一合理的实现。在这个问题的答案的评论中有人说C++11 要求为尾随std::string分配额外的char'\0'. 所以看来第二种选择是可能的。
另一个人说,std::string操作——例如迭代、串联和元素变异——不需要零终止符。除非您将 传递string给需要以零结尾的字符串的函数,否则可以省略它。
更多来自专家的声音:
为什么实现者通常让 .data() 和 .c_str() 做同样的事情?
因为这样做效率更高。使 .data() 返回非 null 终止的内容的唯一方法是让 .c_str() 或 .data() 复制其内部缓冲区,或者仅使用 2 个缓冲区。拥有一个以 null 结尾的缓冲区始终意味着您在实现 std::string 时始终可以仅使用一个内部缓冲区。
string::c_str()所以我现在真的很困惑,调用时实际上做了什么?
更新:
如果c_str()实现为简单地返回指针,则它已经被分配和管理。
A。由于c_str()必须以 null 终止,因此内部缓冲区需要始终以 null 终止,即使对于空的 std::string,例如: ;的内部存储器中std::string demo_str应该有一个。我对吗?\0demo_str
B.std::string::substr()调用时会发生什么自动将 a …
在我的计算机科学高级项目中,我正在制作一款益智视频游戏,该游戏将教人们如何使用 Python 进行编码。设计的最大部分涉及创建用户可以分类的引人入胜的谜题。然而,拼图设计不是我目前遇到的问题。
虽然我最初的想法是让每个“问题”都有一系列预期的答案来检查用户的答案,但我学校的 CS 部门负责人(主持高级研讨会)建议我改用嵌入式 Python,他建议两者都进行挑战me,并使数据更容易嵌入一个简单的解释器,该解释器将根据预期输出检查用户代码输出。
快进四个星期,我学到了很多关于 Python 实际工作原理的知识。我什至让解释器将简单的 C 字符串作为 python 代码,比如 print("hello") 或 print("hello/tworld"),只要 C 字符串中没有空格。
该代码看起来像
#include "pch.h"
#include <iostream>
#include <string>
#include <fstream>
#include <Python.h>
void exec_pycode(const char* code);
int main()
{
using namespace std;
Py_Initialize();
string input;
cin >> input;
/*Though the variable gets passed to a const char* definition,
the pointer is constant over the character, because the pointer ALWAYS points
to the same function*/
char const *PyCode = input.data();
exec_pycode(PyCode); …Run Code Online (Sandbox Code Playgroud) 我应该如何编写一个高效的异常类来显示可以通过在运行时修复源代码错误来防止的错误?
这就是我选择的原因std::invalid_argument。
我的异常类(显然不起作用):
class Foo_Exception : public std::invalid_argument
{
private:
std::string Exception_Msg;
public:
explicit Foo_Exception( const std::string& what_arg );
virtual const char* what( ) const throw( );
};
explicit Foo_Exception::Foo_Exception( const std::string& what_arg ) // how should I write this function???
{
Exception_Msg.reserve( 130000 );
Exception_Msg = "A string literal to be appended, ";
Exception_Msg += std::to_string( /* a constexpr int */ );
Exception_Msg += /* a char from a const unordered_set<char> */;
}
const char* Foo_Exception::what( ) …Run Code Online (Sandbox Code Playgroud)