运行这段代码:
#include <string>
#include <iostream>
using namespace std;
int main() {
string a = "Hello, world!";
cout << (-1)*a.size() << endl;
}
Run Code Online (Sandbox Code Playgroud)
我得到:
18446744073709551603
Run Code Online (Sandbox Code Playgroud)
这显然不是我想要的。每当我尝试将 string::size() 乘以负数(但不是正数!)时,都会发生这个恼人的错误。我不明白这里出了什么问题。
我公司使用这样的代码:
std::string(CT2CA(some_CString)).c_str()
Run Code Online (Sandbox Code Playgroud)
我认为它将Unicode字符串(其类型为CString)转换为ANSI编码,此字符串用于电子邮件的主题.但是,电子邮件的标题(包括主题)表示邮件客户端应将其解码为unicode(这是原始代码的作用).因此,一些像"äöü"这样的德国字母将无法正确显示为标题.
无论如何,我可以将此标头放回UTF8并存储到std :: string或const char*?
我知道有很多更聪明的方法可以做到这一点,但我需要保持代码坚持原来的(即将头部发送为std :: string或const char*).
提前致谢.
我对以下代码感到困惑:
string _str = "SDFDFSD";
char* pStr = (char*)_str.data();
for (int i = 0; i < iSize; i++)
pStr[i] = ::tolower(pStr[i]);
Run Code Online (Sandbox Code Playgroud)
这里_str.data()回来了const char*.但是我们将它分配给一个char*.我的问题是,
_str.data()返回指向常量数据的指针.如何将它存储在指向数据的指针中?数据是不变的吗?如果我们将它分配给char指针而不是像我们在for语句中那样改变它,这对于常量数据是不可能的.
如果s1和s2是字符串,那么(据我所知)
s1 = s2.substr();
Run Code Online (Sandbox Code Playgroud)
意思完全一样
s1 = s2;
Run Code Online (Sandbox Code Playgroud)
为什么有人想在substr()没有任何参数的情况下打电话?
编辑:表达同一问题的另一种方式:
为什么标准substr如此定义:
basic_string substr( size_type pos = 0,
size_type count = npos ) const;
Run Code Online (Sandbox Code Playgroud)
而不是这样:
basic_string substr( size_type pos,
size_type count = npos ) const;
Run Code Online (Sandbox Code Playgroud) 我有一个接受 (char* const*) 的函数。我提供的数据在 std::string 中。所以我这样做:
void blahblah(std::string str)
{
const char * cc = str.c_str();
ConstCharFunction(&cc);
}
Run Code Online (Sandbox Code Playgroud)
这很好用。我的问题是我是否需要清理 const char ala 使用的内存:
delete cc;
Run Code Online (Sandbox Code Playgroud)
或者 cc 只是一个指向 std:string 中内存位置的指针...
我有以下代码,但无法编译。我想不出原因,请帮助。
rapidjson::Document jsonDoc;
jsonDoc.SetObject();
rapidjson::Document::AllocatorType& allocator = jsonDoc.GetAllocator();
rapidjson::Value messageArr(rapidjson::kArrayType);
std::string test = std::string("TEST");
messageArr.PushBack(test.c_str(), allocator);
Run Code Online (Sandbox Code Playgroud)
给我以下错误;
错误:没有匹配的函数调用 'rapidjson::GenericValue >::PushBack(const char*, Rapidjson::GenericDocument >::AllocatorType&)'
messageArr.PushBack(test.c_str(), allocator);
std::string从a 做一个char.例如,不会编译,
#include <string>
int main()
{
char c = 'a';
std::string s(c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在g ++中,我得到了这么多错误,
foo.cpp: In function 'int main()':
foo.cpp:6:20: error: no matching function for call to 'std::__cxx11::basic_strin
g<char>::basic_string(char&)'
std::string s(c);
^
In file included from i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/in
clude/c++/string:52:0,
from foo.cpp:1:
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:535:9: note: candidate: template<class _InputIterator> std::__cxx11::bas
ic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator,
const _Alloc&)
basic_string(_InputIterator __beg, _InputIterator __end,
^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:535:9: note: template argument deduction/substitution failed:
foo.cpp:6:20: note: candidate expects 3 arguments, …Run Code Online (Sandbox Code Playgroud) 我的任务是在 Linux 中编写一个文件系统,该系统将存储设备模拟为文件。
我被赋予的功能之一需要读取“文件”中的数据。为此,我有一个应该返回std::string.
如何使用以下函数将文件中的数据直接读入std::string?
void read(int addr, int size, char *ans)
Run Code Online (Sandbox Code Playgroud)
有没有办法将内部的字符数组std::string作为参数传递给这个函数?到目前为止,我遇到的所有方法和成员函数都只返回const char*,在这种情况下不起作用。
谢谢!
所以,我是 C++ 的新手,我无法弄清楚为什么会发生这种情况。我有一个包含所有字母的字符串,我将 10 个字符从它复制到一个新字符串中s2,使用 for 循环逐个字符复制,如下所示,当我执行它时,该cout函数正在打印一个空行。
#include <iostream>
using namespace std;
int main(){
string s = "abcdefghijklmnopqrstuvwxyz";
string s2;
for(int i=0; i<10; i++){
s2[i] = s[i];
}
cout << s2 << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当我s2逐个字符打印这个字符串时,我得到了正确的输出
#include <iostream>
using namespace std;
int main(){
string s = "abcdefghijklmnopqrstuvwxyz";
string s2;
for(int i=0; i<10; i++){
s2[i] = s[i];
}
for(int i=0; i<10; i++){
cout << s2[i];
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!
我已经使用 cpp 很长一段时间了,我知道我们不能添加字符串和数字(因为 + 运算符没有为此重载)。但是,我看到了这样的代码。
#include <iostream>
using namespace std;
int main() {
string a = "";
a += 97;
cout << a;
}
Run Code Online (Sandbox Code Playgroud)
这输出'a',我也试过这个。
string a ="";
a=a+97;
Run Code Online (Sandbox Code Playgroud)
第二个代码给出了一个编译错误(作为 + 运算符std::string和 的无效参数 int)。我不想连接字符串和数字。有什么不同?为什么一个有效,而另一个无效?
我原以为a+=97是一样的,a=a+97但它似乎不同。