-
3 votesanswersviews
了解在共享库中重载operator new的行为
我正在Centos平台上工作一个共享库和应用程序[clang,llvm3.9.0和libc],并且库和应用程序都会重载它们自己的operator new和operator delete . 除1例外,一切正常 . 在调用std :: string的copy构造函数时总是调用应用程序端的 operator new : 这是senario: std::string str1 ( "A ver... -
1 votesanswersviews
将文件读入内存C:是否有std :: strings的getline()
我被要求更新我的代码,该代码读取文本文件并解析它以获取特定的字符串 . 基本上不是每次都打开文本文件,我想将文本文件读入内存并在对象的持续时间内使用它 . 我想知道是否有类似getline()的函数我可以用于std :: string,就像我可以用于std :: ifstream . 我意识到我可以使用while / for循环,但我很好奇是否还有其他方法 . 这是我目前正在做的事情: file.... -
411 votesanswersviews
如何替换字符串中出现的所有字符?
在 std::string 中用字符替换所有出现的字符的有效方法是什么? -
136 votesanswersviews
用于将整数转换为字符串C的itoa()的替代方案? [重复]
这个问题在这里已有答案: Easiest way to convert int to string in C++ 25个答案 我想知道是否有一个替代 itoa() 用于将整数转换为字符串,因为当我在visual Studio中运行它时会收到警告,当我尝试在Linux下构建我的程序时,我收到编译错误 . -
0 votesanswersviews
const char *和字符串比较如何在C中起作用?
请考虑以下代码段: #include <iostream> int main() { std::string str = "Hello"; const char *cstr = "Hello"; if (cstr == str) { std::cout<<"Both are same ... -
3 votesanswersviews
使用bool初始化std :: string
考虑以下初始化: std::string falseString = false; std::string trueString = true; 使用 g++ 5.2.0 时,编译器会为 falseString 发出警告,而 trueString 则会出错 . 使用 clang++ 3.6 -std=c++11 ,编译器会为 falseString 和 trueString 抛出错误 . Q1)... -
583 votesanswersviews
如何连接std :: string和int?
我认为这很简单,但它会带来一些困难 . 如果我有 std::string name = "John"; int age = 21; 如何组合它们以获得单个字符串 "John21" ? -
12 votesanswersviews
map <string,string>如何在此 Map 中插入数据?
我需要以键值格式存储字符串 . 所以我正在使用如下的 Map . #include<map> using namespace std; int main() { map<string, string> m; string s1 = "1"; string v1 = "A"; m.insert(pai... -
676 votesanswersviews
修剪std :: string的最佳方法是什么?
我目前正在使用以下代码来修正程序中的所有 std::strings : std::string s; s.erase(s.find_last_not_of(" \n\r\t")+1); 它工作正常,但我想知道是否有一些可能会失败的最终案例? 当然,欢迎使用优雅替代品和左侧修饰解决方案的答案 . -
3 votesanswersviews
避免在没有原始指针的情况下复制 Map 的密钥
每次在std :: map中插入一对,其键是std :: string时,它会生成两个副本 . 您可以避免使用原始指针,但它是异常不安全的 . 有没有办法使用智能指针而不是原始指针? 示例代码: // To compile: g++ -std=c++0x exmaple.cpp -o example #include <iostream> #include <string&g...