我在我的代码库中遇到了一个棘手的难题.我不知道为什么我的代码会生成此错误,但(例如)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) 我有一个字符串
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函数内使用(读/写)
我知道有一个类似的标题问题,但我想知道我对这个具体案例的选择.
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字符串(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 :: …
我试着这样做:
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) 我想使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");将被循环捕获,直到前一个附加完成.
这个解决方案太天真了吗?
我的一位同事声称,实施特别是内存管理std::string不如QStringQt库中的实现.这std::string也可以指出哪些方面?
的std::string存取器(back,front,at,和operator[])具有const和非const过载,如下:
char& x();
const char& x() const;
Run Code Online (Sandbox Code Playgroud)
为什么第二个版本返回const引用,而不是简单地返回char by值(作为副本)?
根据关于如何传递对象的经验法则,当不需要修改原始对象时,不应该通过值传递小对象吗?
#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)
cout<<" Your reverse name is "<<name[i]<<endl;假设我将std::string::reservean的容量设置std::string为20。该字符串的长度会更改为20还是保持为零?