标签: stdstring

C++加法过载模糊

我在我的代码库中遇到了一个棘手的难题.我不知道为什么我的代码会生成此错误,但(例如)std :: string则不然.

class String {
public:
    String(const char*str);
    friend String operator+ ( const String& lval, const char *rval );
    friend String operator+ ( const char *lval, const String& rval );
    String operator+ ( const String& rval );
};
Run Code Online (Sandbox Code Playgroud)

这些的实现很容易想象你自己.

我的驱动程序包含以下内容:

String result, lval("left side "), rval("of string");
char lv[] = "right side ", rv[] = "of string";
result = lv + rval;
printf(result);
result = (lval + rv);
printf(result);
Run Code Online (Sandbox Code Playgroud)

这在gcc 4.1.2中生成以下错误:

driver.cpp:25: error: ISO C++ says that these are …
Run Code Online (Sandbox Code Playgroud)

c++ gcc operator-overloading stdstring

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

如何有效地将std :: string复制到向量中

我有一个字符串

std::string s = "Stack Overflow";
Run Code Online (Sandbox Code Playgroud)

我需要复制到矢量中.这就是我这样做的方式

std::vector<char> v;
v.reserve(s.length()+1);
for(std::string::const_iterator it = s.begin(); it != s.end(); ++it)
{
    v.push_back( *it );
}
v.push_back( '\0' );
Run Code Online (Sandbox Code Playgroud)

但我听说范围操作更有效率.所以我在想这样的事情

std::vector<char> v( s.begin(), s.end());
v.push_back('\0');
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,这样做会更好吗?插入'\ 0'时潜在的重新分配怎么样?
我想的另一种方法就是这样

std::vector<char> v(s.length()+1);
std::strcpy(&v[0],s.c_str());
Run Code Online (Sandbox Code Playgroud)

也许速度快但可能不安全?
编辑
必须是一个空终止字符串,可以在C函数内使用(读/写)

c++ stdstring stdvector

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

C++ std :: string替代strcpy?

我知道有一个类似的标题问题,但我想知道我对这个具体案例的选择.

MSVC编译器发出有关strcpy的警告:

1>c:\something\mycontrol.cpp(65): warning C4996: 'strcpy': This function or
variable may be unsafe. Consider using strcpy_s instead. To disable
deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

void MyControl::SetFontFace(const char *faceName)
{
    LOGFONT lf;

    CFont *currentFont = GetFont();
    currentFont->GetLogFont(&lf);
    strcpy(lf.lfFaceName, faceName); <--- offending line
    font_.DeleteObject();
    // Create the font.
    font_.CreateFontIndirect(&lf);

    // Use the font to paint a control.
    SetFont(&font_);
}
Run Code Online (Sandbox Code Playgroud)

注意font_是一个实例变量.LOGFONT是一个windows结构,lfFaceName定义为TCHAR lfFaceName[LF_FACESIZE].

我想知道的是我可以做以下事情(如果不是为什么不这样做):

void MyControl::SetFontFace(const std::string& faceName)
...
  lf.lfFaceName = faceName.c_str(); …
Run Code Online (Sandbox Code Playgroud)

c++ stdstring strcpy

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

C ++将字符串隐式转换为char *匹配错误的函数签名

我正在编写一个程序,该程序应该处理c字符串(char *)和c ++字符串(std :: string)。我出于关注而孤立了以下示例。

#include <iostream>
#include <string>

void hello(std::string s) {
    std::cout << "STRING FUNCTION" << std::endl;
}

void hello(char* c) {
    std::cout << "CHAR FUNCTION" << std::endl;
}

int main(int argc, char* argv[]) {
    hello("ambiguous");
    hello((std::string)"string");
    hello((char*)"charp");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我编译该程序时,我得到警告:

test.cpp:14: warning: deprecated conversion from string constant to ‘char*’
Run Code Online (Sandbox Code Playgroud)

关于首次致电hello。运行该程序可以得到:

CHAR FUNCTION
STRING FUNCTION
CHAR FUNCTION
Run Code Online (Sandbox Code Playgroud)

显示对的第一个调用hello与签名匹配hello(char* c)。

我的问题是,如果作为c ++程序,字符串文字("ambiguous")是std :: string,为什么将其强制转换为a char*然后匹配函数,hello(char* c)而不是停留在std :: …

c++ char stdstring implicit-cast

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

如何将字符串添加到 Windows 窗体标签?

我试着这样做:

this->Label1->Text = "blah blah: " + GetSomething();
Run Code Online (Sandbox Code Playgroud)

哪里GetSomething()是返回字符串的函数。

编译器给了我一个错误:

“错误 C2679:二进制 '+':未找到采用 'std::string' 类型的右侧操作数的运算符(或没有可接受的转换)”

string GetSomething()
{
    int id = 0;
    string Blah[] = {"test", "fasf", "hhcb"};

    return Blah[id];
}
Run Code Online (Sandbox Code Playgroud)

.net string c++-cli stdstring winforms

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

std :: string追加线程安全天真的解决方案?

我想使std :: string追加函数线程安全,因为我从不同的线程追加一个特定的字符串.

我是c ++的初学者,所以我不确定这可能会出现什么问题.

我正在考虑的解决方案是使用而不是使用 somestring.append("appendthis");

使用以下代码:

bool appendtsReady = true;
void appendts(std::string& originalString, char* app)
{
    while (!appendtsReady) {}

    appendtsReady = false;
    originalString.append(app);
    appendtsReady = true;
}

appendts(somestring, "appendthis");
Run Code Online (Sandbox Code Playgroud)

我希望如果附加字符串,新请求 appendts(somestring, "appendthis_from_different_thread");将被循环捕获,直到前一个附加完成.

这个解决方案太天真了吗?

c++ string multithreading stdstring c++11

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

std :: string是否比QString差?

我的一位同事声称,实施特别是内存管理std::string不如QStringQt库中的实现.这std::string也可以指出哪些方面?

c++ qstring qt stdstring

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

为什么std :: string的const访问器返回引用?

的std::string存取器(back,front,at,和operator[])具有const和非const过载,如下:

char& x();
const char& x() const;
Run Code Online (Sandbox Code Playgroud)

为什么第二个版本返回const引用,而不是简单地返回char by值(作为副本)?

根据关于如何传递对象的经验法则,当不需要修改原始对象时,不应该通过值传递小对象吗?

c++ stl language-design stdstring c++11

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

用C++反转字符串

#include <iostream>
#include <cstdlib>
using namespace std;

main() 
{
beginning:
    string name;
    cout << "Hey! Enter your name" << endl;
    cin >> name;
    int i = name.length() - 1;
    do
    {
        cout << name[i];
        i--;
    } while (i > -1);
    cout << " Your reverse name is " << name[i] << endl;
    cout << name[i] << " Your reverse name is " << endl;
    goto beginning;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  1. 为什么在"你的反向名称"之前显示 "zsuidakrA ",尽管我已经编码了cout<<" Your reverse name is "<<name[i]<<endl;
  2. 对于 …

c++ reverse stdstring

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

std :: string :: reserve是否更改std :: string的大小?

假设我将std::string::reservean的容量设置std::string为20。该字符串的长度会更改为20还是保持为零?

c++ stl stdstring

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