可以跨使用不同版本的 Visual Studio 构建的 DLL 之间的 DLL 边界按值传递 std::string 吗?
我遇到过这种模式的多个实例(boost::filesystem 仅用作示例):
boost::filesystem::path path = ...;
someFunctionTakingCStrings(path.string().c_str());
Run Code Online (Sandbox Code Playgroud)
在哪里
const std::string path::string() const
{
std::string tmp = ...
return tmp;
}
Run Code Online (Sandbox Code Playgroud)
虽然我从未遇到过这种模式的问题,但我想知道返回的字符串何时sting()被销毁,以及访问 的代码是否c_str()安全,因为c_str() 生命周期绑定到 std::string 生命周期。
我试图将一个 std::string 转换为以下格式的rapidJson 对象
{
"data":{
"value": "AB1234"
}
}
Run Code Online (Sandbox Code Playgroud)
我努力了
rapidjson::Document aJsonDocument;
aJsonDocument.SetObject();
rapidjson::Document::AllocatorType &aAllocator = aJsonDocument.GetAllocator();
rapidjson::Value aPsmJson(rapidjson::kStringType);
std::string aStr = "ABCDEF";
aPsmJson.SetString(aStr.c_str(), aAllocator);
aJsonDocument.AddMember("value", aPsmJson, aAllocator);
//jsonToString is a function to convert json document to string
std::string aInputJsonString = jsonToString(aJsonDocument);
std::cout << "Output: " << aInputJsonString ;
Run Code Online (Sandbox Code Playgroud)
这给出了输出 {"value":"ABCDEF"}
我有一个模拟窗口的程序;所以我将窗口的内容存储在content一种std::string类型的成员数据中:
class Window {
using type_ui = unsigned int;
public:
Window() = default;
Window(type_ui, type_ui, char);
void print()const;
private:
type_ui width_{};
type_ui height_{};
char fill_{};
std::string content_{};
mutable type_ui time_{};
};
Window::Window(type_ui width, type_ui height, char fill) :
width_{ width }, height_{ height }, fill_{ fill },
content_{ width * height, fill } { // compile-time error here?
//content( width * height, fill ) // works ok
}
void Window::print()const {
while (1) {
time_++;
for …Run Code Online (Sandbox Code Playgroud) I am 4 hours-new to C++ and have hit a brick wall with string vectors. When trying to add multiple strings to a string vector, I keep erroring out. I'd like to use push_back.
I would also like to display this string vector, but I'm not sure how (I know how to display non-vectors). Given that I have not been able to add a string to a vector of strings, I did not attempt to display the vector of …
我熟悉,appendinstd::string返回std::string&,因此它没有资格被移出,所以结果不会被移动。
#include <string>
int main()
{
std::string s = std::string("A").append("B");
return s.size();
}
Run Code Online (Sandbox Code Playgroud)
#include <string>
int main()
{
std::string s = std::move(std::string("A").append("B"));
return s.size();
}
Run Code Online (Sandbox Code Playgroud)
在那里你可以看到,后一个例子将产生一个更少的分配,因此在这种情况下,最好移动一些看起来像临时的东西。我的问题是,为什么他们(委员会)不添加简单的过载&&,使结果append要么std::string&,或者std::string&&根据上下文?我的意思是类似于std::optional正在做的事情value。有没有一个例子可以证明这种优化是假的?
考虑以下struct用户定义的转换函数,该函数可以将自身转换为const char*;
struct S {
operator const char*() { return "hello"; }
};
Run Code Online (Sandbox Code Playgroud)
与此一起使用<iostream>,我们可以打印struct S没有错误消息的内容:
std::cout << S{} << '\n';
Run Code Online (Sandbox Code Playgroud)
但是如果我将返回类型更改为std::string:
struct S {
operator std::string() { return "hello"; }
};
Run Code Online (Sandbox Code Playgroud)
我收到此编译器错误消息:
<source>:11:13: error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'S')
11 | std::cout << S{} << '\n';
| ~~~~~~~~~ ^~ ~~~
| | |
| | S
| std::ostream {aka std::basic_ostream<char>}
<source>:11:18: note: 'S' …Run Code Online (Sandbox Code Playgroud) 使用 C++,我想创建一个 for 循环如下(text是一个std::string):
for(int i = text.size(); i >= 0; i--) {
{
Run Code Online (Sandbox Code Playgroud)
请有人帮助我理解为什么我的编译器(Xcode)要求我初始化i为无符号长整数,而不是整数?
我假设以下内容,但我不知道,我想更好地理解:我相信它与最大值为 2147483647 的整数有关,而 unsigned long 的最大值为 18,446,744,073,709,551,615。由于一个字符串最多可以容纳 4294967295 个字符,所以使用整数是不合适的,因为它的最大值不能代表字符串的最大长度?
最后,unsigned long 数据类型是否合适,还是应该使用 long int?
我修改后的 for 循环如下:
for(unsigned long i = text.size(); i >= 0; i--) {
}
Run Code Online (Sandbox Code Playgroud)
或者
for(long int i = text.size(); i >= 0; i--) {
}
Run Code Online (Sandbox Code Playgroud) 假设我有以下代码:
void some_function(std::string_view view) {
std::cout << view << '\n';
}
int main() {
some_function(std::string{"hello, world"}); // ???
}
Run Code Online (Sandbox Code Playgroud)
viewinside会some_function指的string是已被破坏的吗?我很困惑,因为考虑到这段代码:
std::string_view view(std::string{"hello, world"});
Run Code Online (Sandbox Code Playgroud)
产生警告(来自clang++):
warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl]
Run Code Online (Sandbox Code Playgroud)
有什么不同?
(奇怪的是,使用大括号{}而不是方括号()来初始化string_view上面的内容消除了警告。我也不知道为什么会这样。)
需要明确的是,我理解上面的警告( 的string_view寿命比 长string,所以它持有一个悬空指针)。我要问的是为什么传递 astring不会some_function产生相同的警告。
我正在使用一个 C 库,它使用各种固定大小的unsigned char数组,不带空终止符作为字符串。
我已经将它们转换为std::string使用以下函数:
auto uchar_to_stdstring(const unsigned char* input_array, int width) -> std::string {
std::string temp_string(reinterpret_cast<const char*>(input_array), width);
temp_string.erase(temp_string.find_last_not_of(' ') + 1);
return temp_string;
}
Run Code Online (Sandbox Code Playgroud)
除了使用reinterpret_cast、需要传递数组大小以及我将数组衰减为指针之外,它工作得很好。我试图通过使用 来避免所有这些问题std::span。
使用的函数std::span如下所示:
auto ucharspan_to_stdstring(const std::span<unsigned char>& input_array) -> std::string {
std::stringstream temp_ss;
for (const auto& input_arr_char : input_array) {
temp_ss << input_arr_char;
}
return temp_ss.str();
}
Run Code Online (Sandbox Code Playgroud)
该函数运行良好,使其他一切变得更简单,而无需跟踪 C 数组的大小。但是,通过一些基准测试(使用nanobench )进一步挖掘表明,新函数比经典方法慢很多倍reinterpret_cast。我的假设是基于 - 的函数for中的循环std::span是这里效率低下的。
我的问题:是否有更有效的方法将固定大小的无符号字符 C 数组从 …