在C++中转换int
为等效的最简单方法是什么?string
我知道两种方法.有没有更简单的方法?
(1)
int a = 10;
char *intStr = itoa(a);
string str = string(intStr);
Run Code Online (Sandbox Code Playgroud)
(2)
int a = 10;
stringstream ss;
ss << a;
string str = ss.str();
Run Code Online (Sandbox Code Playgroud) 什么是"序列点"?
未定义的行为和序列点之间的关系是什么?
我经常使用有趣和复杂的表达方式a[++i] = i;
,让自己感觉更好.我为什么要停止使用它们?
如果您已阅读此内容,请务必访问后续问题重新加载未定义的行为和序列点.
(注意:这是Stack Overflow的C++常见问题解答的一个条目.如果你想批评在这种形式下提供常见问题解答的想法,那么发布所有这些的元数据的发布将是这样做的地方.这个问题在C++聊天室中受到监控,其中FAQ的想法一开始就出现了,所以你的答案很可能被那些提出想法的人阅读.)
当创建a的新实例MyClass
作为函数的参数时,如下所示:
class MyClass
{
MyClass(int a);
};
myFunction(MyClass(42));
Run Code Online (Sandbox Code Playgroud)
该标准是否使得任何被授权者都能获得析构函数的时间安排?
具体来说,我可以假设在调用之后的下一个语句之前调用它myFunction()
吗?
以下C++代码是否格式良好:
void consumer(char const* p)
{
std::printf("%s", p);
}
std::string random_string_generator()
{
// returns a random std::string object
}
consumer(random_string_generator().c_str());
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,在创建临时std :: string对象并获取c_str()指针后,没有什么能阻止std :: string对象被破坏(或者我错了?).如果代码一切正常,你能指点我的标准吗?当我使用g ++进行测试时,它确实有效.
我测试了这段代码:
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string s1("a"),s2("b");
const char * s = (s1+s2).c_str();
printf("%s\n",s);
}
Run Code Online (Sandbox Code Playgroud)
它返回"ab".
据我所知,因为它(s1 +s2)
是一个临时对象,可能会以某种方式消失(我不知道),然后const char * s
可能指向未定义的内存并可能被转储.
那么使用.c_str()
那样安全吗?
可能重复:
临时生活
int LegacyFunction(const char *s) {
// do something with s, like print it to standard output
// this function does NOT retain any pointer to s after it returns.
return strlen(s);
}
std::string ModernFunction() {
// do something that returns a string
return "Hello";
}
LegacyFunction(ModernFunction().c_str());
Run Code Online (Sandbox Code Playgroud)
可以轻松地重写上面的示例以使用智能指针而不是字符串; 我多次遇到过这两种情况.无论如何,上面的例子将在ModernFunction中构造一个STL字符串,返回它,然后获取一个指向字符串对象内部的C风格字符串的指针,然后将该指针传递给遗留函数.
下面的代码导致未定义的行为.请务必阅读所有答案的完整性.
通过operator<<
我链接对象时,我想保留对象的左值/右值:
class Avenger {
public:
Avenger& operator<<(int) & {
return *this;
}
Avenger&& operator<<(int) && {
return *this; // compiler error cannot bind lvalue to rvalue
return std::move(*this);
}
};
void doJustice(const Avenger &) {};
void doJustice(Avenger &&) {};
int main() {
Avenger a;
doJustice(a << 24); // parameter should be Avenger&
doJustice(Avenger{} << 24); // parameter should be Avenger&&
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不能简单地返回*this
这意味着类型*this
的的rvalue
对象仍然是一个lvalue reference
.我原以为是rvalue reference …
我发现以下方案延长了临时工作的寿命,我不知道是否应该,但确实如此.
struct S {
std::vector<int>&& vec;
};
int main() {
S s1{std::vector<int>(5)}; // construct with temporary
std::cout << s1.vec[0] << '\n'; // fine, temporary is alive
}
Run Code Online (Sandbox Code Playgroud)
但是,当S
给定显式值构造函数时,它不再是聚合,并且此方案失败并且读取无效s1.vec[0]
struct S {
std::vector<int>&& vec;
S(std::vector<int>&& v)
: vec{std::move(v)} // bind to the temporary provided
{ }
};
int main() {
S s1{std::vector<int>(5)}; // construct with temporary
std::cout << s1.vec[0] << '\n'; // not ok. invalid read on free'd memory
}
Run Code Online (Sandbox Code Playgroud)
为什么这对聚合有效?我认为它与构造函数是一个实际的函数调用有关,基于我使用const lvalue refs的红色.另外,有没有办法让后一种情况起作用?
在SO上使用左值引用处理类似情况有很多问题.我看到如果我使用了const lvalue ref,那么延长临时值的生命周期就无济于事了,rvalue refs的规则是否相同?
可能重复:
C++:临时参数的生命周期?
据说临时变量作为评估完整表达式的最后一步被破坏,例如
bar( foo().c_str() );
Run Code Online (Sandbox Code Playgroud)
临时指针一直存在,直到bar返回,但是为什么
baz( bar( foo().c_str() ) );
Run Code Online (Sandbox Code Playgroud)
是否它仍然存在直到bar返回,或者baz返回意味着完全表达结束在这里,编译器我检查了baz返回后的destruct对象,但我可以依赖它吗?
在阅读C++ 11中的多线程时,我注意到一些教程这样做:
std::thread(print_message, "Hello").detach();
// instead of...
std::thread t(print_message, "Hello");
t.detach();
Run Code Online (Sandbox Code Playgroud)
我的问题是:
std::thread
?