当使用std :: string的at()方法时,我有一个奇怪的问题.我想使用这个库计算给定字符串的md5哈希值:http://sourceforge.net/projects/libmd5-rfc/files/ 正确计算哈希值,但是以人工方式打印时出现问题.输出是:
af04084897ebbf299b04082d105ab724
ffffffaf040848ffffff97ffffffebffffffbf29ffffff9b04082d105affffffb724
Run Code Online (Sandbox Code Playgroud)
代码是:
#include <stdio.h>
#include<string>
#include<iostream>
extern "C" {
#include "md5.h"
}
int main()
{
md5_state_t state;
md5_byte_t digest[16];
std::string callid("f83bc385-26da-df11-95d5-0800275903dd@pc-archdev");
md5_init(&state);
md5_append(&state, (const md5_byte_t*)callid.c_str(), callid.length());
std::string callid_digest((const char*)digest, 16);
for(int i = 0; i < 16; ++i) {
printf("%02x", digest[i]);
}
printf("\n");
for(int i = 0; i < 16; ++i) {
const char c = callid_digest.at(i);
printf("%02x", c);
}
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
"f"字符来自哪里?
每次在std :: map中插入一对,其键是std :: string时,它会生成两个副本.您可以避免使用原始指针,但它是异常不安全的.有没有办法使用智能指针而不是原始指针?
示例代码:
// To compile: g++ -std=c++0x exmaple.cpp -o example
#include <iostream>
#include <string>
#include <map>
#include <memory>
class StringSquealer: public std::string
{
public:
StringSquealer(const std::string s) : std::string(s) {}
StringSquealer(const StringSquealer&)
{
std::cout << "COPY-CONSTRUCTOR" << std::endl;
}
};
int main()
{
// Inefficient
std::map<StringSquealer,int> m1;
m1[StringSquealer("key")] = 1;
std::cout << "---" << std::endl;
// Exception-unsafe
std::map<StringSquealer*,int> m2;
m2[new StringSquealer("key")] = 1;
//Ideal??
std::map<std::unique_ptr<StringSquealer>,int> m3;
std::unique_ptr<StringSquealer> s(new StringSquealer("key"));
//!m3[std::move(s)] = 1; // No compile
} …Run Code Online (Sandbox Code Playgroud) 我正在编写自己的字符串类,仅仅是为了学习和巩固一些知识.我有一切工作,除了我想有一个使用移动语义与std :: string的构造函数.
在我的构造函数中,我需要复制并清空std :: string数据指针和其他东西,它需要保留为空但有效的状态,而不删除字符串指向的数据,我该怎么做?
到目前为止,我有这个
class String
{
private:
char* mpData;
unsigned int mLength;
public:
String( std::string&& str)
:mpData(nullptr), mLength(0)
{
// need to copy the memory pointer from std::string to this->mpData
// need to null out the std::string memory pointer
//str.clear(); // can't use clear because it deletes the memory
}
~String()
{
delete[] mpData;
mLength = 0;
}
Run Code Online (Sandbox Code Playgroud) c++ stdstring dynamic-memory-allocation move-semantics c++11
我有以下功能:
void MyLib::sendMessage(const std::string& message) {
m_xIOService.post( boost::bind(&VoIPPhone::onSendMessage, this, message) );
}
void MyLib::onSendMessage(const std::string& message) {
m_xVoIPClient.sendMessage(message);
}
Run Code Online (Sandbox Code Playgroud)
所以我在一个线程中调用sendMessagein,并在主线程中调用onSendMessage.
问题是在这种情况下是否会通过boost复制消息字符串.如果没有 - 我怎样才能将字符串传递给onSendMessage函数并确保没有内存泄漏且消息字符串有效,而不是删除对象?
使用VS 2012.
我正在制作刽子手.无论如何,我有一个函数来获取一个std :: string,其长度与猜测的当前单词的长度相同,但是用下划线填充.(如,空白).
功能:
std::string getBlankWord(std::vector<std::string> words, int currentWordIndex)
{
return std::string(words[currentWordIndex], '_');
}
Run Code Online (Sandbox Code Playgroud)
它被调用的行:
currentGuessingString = getBlankWord(words, index);
Run Code Online (Sandbox Code Playgroud)
单词是std :: vector,index是int.words.size()= 22,index = 0,所以我不知道如何调用,在这种情况下,单词[0]可能是罪魁祸首.无论如何,这一行上的某些内容会抛出std :: out_of_range异常,但我无法找到它.
谢谢.
std::string::reserve()不会分配我作为参数传递的确切空间量.例如,如果我尝试为100个字符保留空间,则保留111个字符.如果我通过200,它预留207. 655,650,1007 1000.
这背后的原因是什么?
程序代码:
std::string mystr;
std::cout << "After creation :" << mystr.capacity() << std::endl;
mystr.reserve(1000);
std::cout << "After reserve() :" << mystr.capacity() << std::endl;
mystr = "asd";
std::cout << "After assignment :" << mystr.capacity() << std::endl;
mystr.clear();
std::cout << "After clear() :" << mystr.capacity() << std::endl;
Run Code Online (Sandbox Code Playgroud)
代码输出:
After creation :15
After reserve() :1007
After assignment :1007
After clear() :1007
Run Code Online (Sandbox Code Playgroud)
(IDE:Visual Studio 2012)
std::basic_string具有以下构造函数,该构造函数使用以下指向的以null结尾的字符串的内容初始化字符串s:
std::basic_string(const CharT* s, const Allocator& alloc = Allocator());
Run Code Online (Sandbox Code Playgroud)
但是构造函数如何事先知道为其内部缓冲区中的字符串保留多少空间?
我可以想到两种方法:
1)它可以首先遍历整个以null结尾的字符串,直到它找到第一个NULL字符,记住它遍历了多少个字符,并将其用作其内部缓冲区的容量并开始复制.
缺点:它必须读取字符串两次,一次用于计算字符,第二次用于复制字符串.
2)它可以在其内部缓冲区中保留一个保守的数量,然后开始复制.如果它击中NULL字符缓冲区用完之前,我们就好了,否则我们需要(一个保守量再次)预留更多的空间,然后重复上述步骤.
缺点:如果字符串相当大,不断重新调整容量的开销可能会变得明显.
那么,理智的std :: basic_string实现做了什么(或者甚至在标准中指定了什么)?
我有一个定义如下的方法:
const std::string returnStringMethod()
{
std::string myString;
// populate myString
return myString;
}
Run Code Online (Sandbox Code Playgroud)
现在,在调用者中,我做了类似这样的事情:
const char * ptr = returnStringMethod().c_str();
Run Code Online (Sandbox Code Playgroud)
正如我所看到的,这是返回一些我没想到的截断字符串.但是,以下工作正常:
std::string str = returnStringMethod();
const char * ptr = str.c_str();
Run Code Online (Sandbox Code Playgroud)
有人可以帮我理解这里发生的事情吗?.
PS:我们每周构建一次代码.我上周提交代码时测试了这个,事情还不错.所以,我真的想知道我在这里可能缺少什么.
谢谢,帕万.
我是一名java程序员,还有一天的C/C++编程.我试图从char*创建一个std :: string
这是我的角色缓冲区 -
char* token = (char*) malloc ((numberOfCharacters) * sizeof (char));
Run Code Online (Sandbox Code Playgroud)
这就是我创建一个std :: string对象的方法 -
std::string strKey (token);
Run Code Online (Sandbox Code Playgroud)
但是这个调用后我需要释放'token'还是strKey引用令牌指向的字符缓冲区?
(这个问题的灵感来自Nicolai Josuttis的CppCon 2017演讲.)
考虑以下源文件(对于对象,而不是完整的程序):
#include <string>
class C {
std::string s_;
public:
C(std::string s) : s_(s) { };
void bar();
};
void foo() {
std::string hello { "The quick brown fox jumped over the lazy dog" };
C c { hello };
c.bar();
}
Run Code Online (Sandbox Code Playgroud)
即使使用-O2(甚至使用-O3),似乎字符串构造函数被调用三次.具体而言,s构造,仅用于构造s_,然后被破坏.我的问题:
s_从ctor的参数构造,而不是构造s?s_从s,看到后者是如何闲置?s构造正确,编译器不能避免构造hello,看看它没有其他用途吗?或者至少离开它?