当我尝试编译以下代码时:
\n\n#include <string.h>\nusing namespace std;\ntypedef std::basic_string<char> foostring;\nfoostring foo = "foo";\nRun Code Online (Sandbox Code Playgroud)\n\n我收到以下错误:
\n\nstringtest.cpp:5: error: expected initializer before \xe2\x80\x98<\xe2\x80\x99 token\nstringtest.cpp:6: error: \xe2\x80\x98foostring\xe2\x80\x99 does not name a type\nRun Code Online (Sandbox Code Playgroud)\n\n我的编译器是: g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1
\n我做错了什么?一旦我弄清楚如何使用它,我打算将其与 Windows TCHAR 一起使用以支持 unicode。
// get the drive letter/network volume portion of the path
std::string drive_path;
if (drive_type == disk_util::drive_type_network)
{
boost::iterator_range<std::string::const_iterator> r = boost::find_nth(path, "/", 3);
if (!r.empty())
drive_path = std::string(path.begin(), r.begin());
}
else
{
size_t i = path.find('/');
drive_path = path.substr(0, i);
}
//path is also a std::string type
Run Code Online (Sandbox Code Playgroud)
问题出在这一行:drive_path = std::string(path.begin(), r.begin());
这是在 VS2013 上成功编译,但它在 VS 2015 中抛出错误 Error: error C2440: '': cannot convert from 'initializer list' to 'std::string'
根据 std::string 构造函数,我们有一个 Range Constructor,它接受迭代器 1 和迭代器 2 来填充字符串。
http://www.cplusplus.com/reference/string/string/string/
//range …Run Code Online (Sandbox Code Playgroud) 我在尝试使用 std::string 时遇到了巨大的困难。该程序编译得很好,但是当我运行该程序时,我收到此错误:error
我在谷歌上到处查看,但没有找到关于如何解决这个问题的信息。我还尝试以不同的方式编写代码,看看这是否能告诉我什么。
std::string yourName;
std::cout << "What is your name?";
std::cin >> yourName;
std::cout << "Hello, " << yourName << std::endl;
Run Code Online (Sandbox Code Playgroud)
尽管重写了,我仍然遇到同样的错误,并且我没有忘记包含 iostream 和字符串头文件。
我正在使用 GCC 编译器,我的编译代码是g++ -std=c++14 test.cpp -o test.exe。我还尝试使用编译代码g++ test.cpp -o test.exe。这并没有修复错误。
我之前在 Stack Overflow 上问过这个问题,但我未能得到清晰易懂的答案,很可能是因为我是一个完全的编程初学者。话虽这么说,有人可以给我一个答案,说明可能导致此问题的原因以及如何用简单的英语解决它吗?
我想创建一个自定义分配器basic_string,允许我获得分配的字符串内部数组的所有权。我的具体用例是 .NET 互操作场景,其中将字符串封送回托管代码的成本很高,因为它需要在特定池中分配字符串(至少在Windows 中),更重要的是堆中数组的所有权必须是转入。我能够成功地为std::vector主要编译器(MSVC、gcc、clang)编写此类自定义分配器并验证其兼容性。我现在尝试使用相同的分配器basic_string,我观察到奇怪的行为,因为所有主要的 STL 实现似乎都没有使用提供的分配器进行第一次分配,特别是前 16 个字节。它遵循我正在使用的代码:
#include <memory>
#include <stdexcept>
#include <vector>
#include <iostream>
// The requirements for the allocator where taken from Howard Hinnant tutorial:
// https://howardhinnant.github.io/allocator_boilerplate.html
template <typename T>
struct MyAllocation
{
size_t Size = 0;
std::unique_ptr<T> Ptr;
MyAllocation() { }
MyAllocation(MyAllocation && other) noexcept
: Ptr(std::move(other.Ptr)), Size(other.Size)
{
other.Size = 0;
}
};
// This allocator keep ownership of the last allocate(n)
template <typename T>
class MyAllocator …Run Code Online (Sandbox Code Playgroud) 如果我理解正确,则string::begin返回指向字符串中第一个元素的指针。如果是这样,我们为什么不直接使用 &str来获取指针。
是否存在使用begin更好的情况,或者我误解了它的功能?
我有一个小问题。我想利用字符串中的双字母大写。我设法编译了一个程序,但没有成功。
#include <iostream>
#include <cctype>
#include <string>
std::string::iterator function(
std::string::const_iterator a,
std::string::const_iterator b,
std::string::const_iterator e)
{
for (; a < b; a++)
{
if (*a == *(a + 1))
{
toupper(*a);
toupper(*(a + 1));
}
}
}
int main()
{
std::string in = "peppermint 1001 bubbles balloon gum", out(100, '*');
auto e = function(in.cbegin(), in.cend(), out.begin());
int n = e - out.begin();
std::string s = out.substr(0, n);
bool b = (s == "pePPermint 1001 buBBles baLLOOn gum");
std::cout << std::boolalpha …Run Code Online (Sandbox Code Playgroud) 我有代码输出对象的最大长度std::string:
string s;
cout<<s.max_size()<<endl;
cout << INT_MAX << endl;
cout<<LLONG_MAX<<endl;
Run Code Online (Sandbox Code Playgroud)
输出 :
4611686018427387897
2147483647
9223372036854775807
Run Code Online (Sandbox Code Playgroud)
如果不难的话,请解释一下为什么 的最大尺寸std::string是 4611686018427387897。这大约是 的一半LLONG_MAX。
我有一个函数,它的参数为QStringView. 除此之外,这必须转换为std::string. 有这个QString功能。toStdString()有没有办法避免潜在昂贵的中间步骤 via QString?
众所周知,C++11 添加了一个 null 终止符std::basic_string(这不计入大多数成员函数)。但是当我阅读cpp ref时,我发现对 UB 的取消引用(这一段与 std::vector 的段落end()几乎相同)。为什么会出现这种情况?或者这是 cpp ref 的错误(请提供此文档以进行验证)?
我尝试过 GNU C++,但不幸的是 __gnu_debug 似乎不包含std::string迭代器检查器。Clang++ 的消毒剂也没有。
我一直在使用C++字符串并尝试使用C函数来加载char *字符串.由于需要作为一个参数,我一定要投它该是这样的:std::stringstrcpy()strcpy()char *
std::string destination;
unsigned char *source;
strcpy((char*)destination.c_str(), (char*)source);
Run Code Online (Sandbox Code Playgroud)
代码工作正常,当我在调试器中运行程序时,*source存储的值destination,但由于某些奇怪的原因,它不会打印出来的语句
std::cout << destination;
我注意到,如果我使用
std::cout << destination.c_str();
该值打印正确,一切都很好.为什么会这样?有没有更好的方法复制unsigned char*或char*进入std::string(stringstreams?)这似乎只有foo.c_str()在复制操作中指定字符串时才会发生.
编辑:要回答"你为什么要这样做?"的问题,我用的strcpy()是一个简单的例子.还有一些时候它比分配更复杂.例如,必须使用strncpy()或std::string从C库char *中将函数传递给函数来将X量的字符串A复制到字符串B中,该库作为缓冲区的参数.