标签: stdstring

如何使用glibc的字符串实现在堆栈上分配std :: string?

int main(void)
{
   std::string foo("foo");
}
Run Code Online (Sandbox Code Playgroud)

我的理解是上面的代码使用默认的allocator来调用new.因此,即使在堆栈上分配了std :: string foo,foo中的内部缓冲区也会在堆上分配.

如何创建完全在堆栈上分配的字符串?

c++ memory heap stack stdstring

17
推荐指数
2
解决办法
1万
查看次数

从gdb设置std :: string变量值?

是否有可能......当调试器在断点处停止时,修改std :: string变量的值而不诉诸于调整当前缓冲区的内存映像等黑客攻击?

例如"set var mystring ="hello world"

c++ gdb stdstring

17
推荐指数
1
解决办法
2865
查看次数

如何检查字符串是否包含char?

您好我有一个我读过的文本文件,我必须知道其中一个字符串是否包含[所以我使用过:

if(array[i] == "[")
Run Code Online (Sandbox Code Playgroud)

但问题是,它是不是[[,所以这是行不通的.

你有什么想法来解决这个问题吗?

谢谢

c++ stdstring

17
推荐指数
3
解决办法
4万
查看次数

什么是Linux等价物:MultiByteToWideChar和WideCharToMultiByte?

我正在使用一个包装std :: wstring的类,这个代码需要跨平台,是否有windows函数的等价物:Linux上的MultiByteToWideChar和WideCharToMultiByte?

谢谢.

c++ linux windows unicode stdstring

16
推荐指数
2
解决办法
1万
查看次数

用于偏移和返回的std :: string :: iterator

  • 我可以通过某个成员将迭代器设置为字符串中的位置5,还是必须执行for(i = 0; i < 5; ++i) iterator++;
  • 给定Iterator,如何将其转换为字符串中的数字偏移量?
  • 如果这不可能用std :: iterators可以提升吗?

迭代器< - >偏移量

c++ iterator stdstring

16
推荐指数
2
解决办法
1万
查看次数

15
推荐指数
2
解决办法
5794
查看次数

如何使用参数集合格式化std :: string?

是否可以格式化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)

c++ string-formatting variadic-functions stdstring

15
推荐指数
2
解决办法
4万
查看次数

显式复制构造函数

我已经扩展了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)

我不确定在哪里确实需要铸造.

如果复制构造函数不是显式的,那么代码可以正常工作,但我想知道并实现显式定义而不是"猜测正确的强制转换".

c++ explicit stdstring copy-constructor assignment-operator

15
推荐指数
2
解决办法
2万
查看次数

C++使用rvalue std :: string编译错误构造对象

我遇到了一个我甚至不知道如何描述的编译错误!这完全让我感到困惑.

情况:

代码尝试使用用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)

c++ constructor std stdstring most-vexing-parse

15
推荐指数
2
解决办法
1239
查看次数

如果我在C++中为"std :: string&"类型的参数指定默认值,是否会导致内存泄漏?

我有一个声明如此的方法:

/*!
\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)

c++ valgrind memory-leaks stdstring

14
推荐指数
2
解决办法
2836
查看次数