int main(void)
{
std::string foo("foo");
}
Run Code Online (Sandbox Code Playgroud)
我的理解是上面的代码使用默认的allocator来调用new.因此,即使在堆栈上分配了std :: string foo,foo中的内部缓冲区也会在堆上分配.
如何创建完全在堆栈上分配的字符串?
是否有可能......当调试器在断点处停止时,修改std :: string变量的值而不诉诸于调整当前缓冲区的内存映像等黑客攻击?
例如"set var mystring ="hello world"
?
您好我有一个我读过的文本文件,我必须知道其中一个字符串是否包含[所以我使用过:
if(array[i] == "[")
Run Code Online (Sandbox Code Playgroud)
但问题是,它是不是[很[,所以这是行不通的.
你有什么想法来解决这个问题吗?
谢谢
我正在使用一个包装std :: wstring的类,这个代码需要跨平台,是否有windows函数的等价物:Linux上的MultiByteToWideChar和WideCharToMultiByte?
谢谢.
for(i = 0; i < 5; ++i) iterator++;?迭代器< - >偏移量
我为什么要打电话std::string::data()过来std::string::c_str()?当然,这里标准的疯狂有一些方法......
是否可以格式化std::string传递一组参数?
目前我正在以这种方式格式化字符串:
string helloString = "Hello %s and %s";
vector<string> tokens; //initialized vector of strings
const char* helloStringArr = helloString.c_str();
char output[1000];
sprintf_s(output, 1000, helloStringArr, tokens.at(0).c_str(), tokens.at(1).c_str());
Run Code Online (Sandbox Code Playgroud)
但是矢量的大小是在运行时确定的.是否有任何类似的函数sprintf_s采用参数集合并格式化std :: string/char*?我的开发环境是MS Visual C++ 2010 Express.
编辑: 我想实现类似的东西:
sprintf_s(output, 1000, helloStringArr, tokens);
Run Code Online (Sandbox Code Playgroud) 我已经扩展了std :: string来满足我不得不将自定义函数构建编写到名为CustomString的字符串类中的需求
我已经定义了构造函数:
class CustomString : public std::string {
public:
explicit CustomString(void);
explicit CustomString(const std::string& str);
explicit CustomString(const CustomString& customString);
//assignment operator
CustomString& operator=(const CustomString& customString);
... };
Run Code Online (Sandbox Code Playgroud)
在第三个构造函数(复制构造函数)和赋值运算符中,其定义为:
CustomString::CustomString(const CustomString& customString):
std::string(static_cast<std::string>(customString))
{}
CustomString& CustomString::operator=(const CustomString& customString){
this->assign(static_cast<std::string>(customString));
return *this;
}
Run Code Online (Sandbox Code Playgroud)
首先,因为这是"明确的"; 意味着需要显式转换以分配给另一个CustomString对象; 它抱怨任务.
CustomString s = CustomString("test");
Run Code Online (Sandbox Code Playgroud)
我不确定在哪里确实需要铸造.
如果复制构造函数不是显式的,那么代码可以正常工作,但我想知道并实现显式定义而不是"猜测正确的强制转换".
我遇到了一个我甚至不知道如何描述的编译错误!这完全让我感到困惑.
情况:
代码尝试使用用char*初始化的rvalue std :: string在堆栈上创建一个对象.
代码:
#include <iostream>
#include <string>
class Foo
{
public:
Foo(double d)
: mD(d)
{
}
Foo(const std::string& str)
{
try
{
mD = std::stod(str);
}
catch (...)
{
throw;
}
}
Foo(const Foo& other)
: mD(other.mD)
{
}
virtual ~Foo() {}
protected:
double mD;
};
class Bar
{
public:
Bar(const Foo& a, const Foo& b)
: mA(a)
, mB(b)
{
}
virtual ~Bar() {}
protected:
Foo mA;
Foo mB;
};
int main(int …Run Code Online (Sandbox Code Playgroud) 我有一个声明如此的方法:
/*!
\brief Removes the leading and trailing white space from a string.
\param s The string to remove the white space from.
\param white_chars Characters to be considered as whitespace.
*/
std::string Trim(const std::string &s, const std::string &white_chars = " \t\n\r");
Run Code Online (Sandbox Code Playgroud)
该方法的定义是无趣的,但无论如何它在这里:
std::string Trim(const std::string &s, const std::string &white_chars)
{
size_t startidx = s.find_first_not_of(white_chars);
if (startidx == std::string::npos) return "";
size_t endidx = s.find_last_not_of(white_chars);
return s.substr(startidx, endidx - startidx + 1);
}
Run Code Online (Sandbox Code Playgroud)
现在在这种方法的大多数用法中,我只提供了第一个参数.Valgrind给了我以下警告
==3338== 68 bytes in 2 blocks …Run Code Online (Sandbox Code Playgroud)