我尝试了两种不同的方式来附加int一个std::string,令我惊讶的是,我得到了不同的结果:
#include <string>
int main()
{
std::string s;
s += 2; // compiles correctly
s = s + 2; // compiler error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么我在使用+=运算符时编译并正常工作,但在使用+运算符时失败?
我不认为问题是如何连接std :: string和int?
在那个问题中,没有答案使用+=operator.And +=
和+operator 之间的区别std::string是解决我怀疑的关键.
坦率地说,这个问题是解释为什么c ++难以掌握的好例子.
我正在做一些维护工作,遇到类似以下的事情:
std::string s;
s.resize( strLength );
// strLength is a size_t with the length of a C string in it.
memcpy( &s[0], str, strLength );
Run Code Online (Sandbox Code Playgroud)
我知道使用&s [0]如果它是std :: vector会是安全的,但是这是std :: string的安全使用吗?
对于常规C字符串,空字符'\0'表示数据的结尾.
那么std::string,我可以使用嵌入空字符的字符串吗?
在C++ 11中,我们知道std::string保证是连续的和以null结尾的(或更迂腐,终止于charT(),在char空字符0 的情况下).
我需要使用这个C API,通过指针填充字符串.它写入整个字符串+ null终止符.在C++ 03中,我总是被迫使用a vector<char>,因为我无法假设它string是连续的或以null结尾的.但是在C++ 11中(假设一个合适的basic_string类,在某些标准库中仍然是不合适的),我可以.
或者我可以吗?当我这样做:
std::string str(length);
Run Code Online (Sandbox Code Playgroud)
该字符串将分配length+1字节,最后一个由null终止符填充.非常好.但是当我把它传递给C API时,它会写出length+1字符.它将覆盖null终止符.
不可否认,它将用空字符覆盖null终止符.赔率是好的,这将工作(事实上,我无法想象它怎么可能不工作).
但我不关心什么"有效".我想知道,根据规范,是否可以用空字符覆盖null终止符?
std::string tmp;
tmp +=0;//compile error:ambiguous overload for 'operator+=' (operand types are 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' and 'int')
tmp +=1;//ok
tmp += '\0';//ok...expected
tmp +=INT_MAX;//ok
tmp +=int(INT_MAX);//still ok...what?
Run Code Online (Sandbox Code Playgroud)
第一个认为传递整数作为参数,对吗?为什么其他人通过编译?我在Visual C++和g ++上测试过,我得到了相同的结果.所以我相信我会错过标准定义的东西.它是什么?
这不是一个设计问题,实际上,虽然看起来像它.(好吧,好吧,这是一个设计问题).我想知道的是为什么C++ std::fstream类不会std::string在构造函数或开放方法中使用它们.每个人都喜欢代码示例:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string filename = "testfile";
std::ifstream fin;
fin.open(filename.c_str()); // Works just fine.
fin.close();
//fin.open(filename); // Error: no such method.
//fin.close();
}
Run Code Online (Sandbox Code Playgroud)
这使我一直处理文件.当然,C++库会std::string尽可能使用?
被std::string使用gcc 4时引用计数-std=c++0x或-std=c++11?
我有以下cout声明.我使用char数组,因为我必须传递vsnprintf给转换变量参数列表并存储Msg.
我们有什么方法可以cout输出到C++ std::string?
char Msg[100];
char appname1[100];
char appname2[100];
char appname3[100];
// I have some logic in function which some string is assigned to Msg.
std::cout << Msg << " "<< appname1 <<":"<< appname2 << ":" << appname3 << " " << "!" << getpid() <<" " << "~" << pthread_self() << endl;
Run Code Online (Sandbox Code Playgroud) 我有一个非常相似的问题
如何使用glibc的字符串实现在堆栈上分配std :: string?
但我认为值得重新询问.
我想要一个std::string溢出到免费商店的本地存储.std::basic_string提供一个分配器作为模板参数,所以看起来要做的就是用本地存储编写一个分配器并用它来参数化basic_string,如下所示:
std::basic_string<
char,
std::char_traits<char>,
inline_allocator<char, 10>
>
x("test");
Run Code Online (Sandbox Code Playgroud)
我试着编写一个inline_allocator可以按照你期望的方式工作的类:它为存储保留10个字节,如果basic_string需要超过10个字节,则调用::operator new().我无法让它发挥作用.在执行上面的代码行的过程中,我的GCC 4.5标准字符串库调用了复制构造函数inline_allocator4次.我不清楚是否有一种合理的方法来编写复制构造函数inline_allocator.
在另一个StackOverflow线程中,Eric Melski将此链接提供给Chromium中的一个类:
http://src.chromium.org/svn/trunk/src/base/stack_container.h
这很有趣,但它不是替代品std::string,因为它包装std::basic_string在一个容器中,所以你必须调用一个重载operator->()来获取std::basic_string.
我找不到任何其他解决方案.难道没有好的解决方案吗?如果这是真的,那么这些std::basic_string和std::allocator概念是否存在严重缺陷?我的意思是,似乎这应该是一个非常基本和简单的用例std::basic_string和std::allocator.我想这个std::allocator概念主要是为游泳池设计的,但我认为它也应该涵盖这个.
看起来像C++ 0x中的rvalue-reference移动语义可能使写入成为可能inline_allocator,如果重写了字符串库,那么basic_string使用其分配器的移动构造函数而不是复制构造函数.有谁知道这个结果的前景如何?
我的应用程序需要每秒构建一百万个微小的ASCII字符串,所以我最终编写了自己的固定长度字符串类Boost.Array,它工作正常,但这仍然困扰着我.
constexpr std::string尝试创建and对象时出现奇怪的编译器错误std::vector:
#include <vector>
#include <string>
int main()
{
constexpr std::string cs{ "hello" };
constexpr std::vector cv{ 1, 2, 3 };
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨“表达式必须具有常量值”:
我错过了什么吗?我使用的是最新的 Microsoft Visual Studio 2019 版本:16.11.4,并且参考(https://en.cppreference.com/w/cpp/compiler_support)指出constexpr此编译器版本支持字符串和向量:
我也尝试过constexpr std::array,它确实有效。该问题是否与向量相关的动态内存分配有关?