将字符串存储在unsigned long int的向量中 [英] Store a string in a vector of unsigned long int

查看:201
本文介绍了将字符串存储在unsigned long int的向量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 std :: string 转换为 std :: vector< unsigned long int> ,并能够逆转它。目前,我这样做:

  std :: string str =Hello World! 
std :: vector< unsigned long int>键;
for(unsigned long int i = 0; i {
unsigned long int key = 0;
for(unsigned long int j = 0; j< sizeof(unsigned long int)&& i< str.length(); ++ i,++ j)
{
unsigned char uchar_value = static_cast< unsigned char>(str [i]);
unsigned long int ulong_value = static_cast< unsigned long int>(uchar_value);
key | = ulong_value<< (j * CHAR_BIT);
}
keys.push_back(key);
}

然后,我有一个类似的过程。但它是相当丑陋! 有更好/更优雅的方式吗?可能直接使用 memcpy 吗?

$ p

<$>

解决方案

p $ p> std :: string str =Hello World!;
std :: vector< unsigned long int> keys(str.size()/ sizeof(unsigned long int)+1);

std :: copy(begin(str),end(str),reinterpret_cast< unsigned char *>(&(* keys.begin())));

反之:

  std :: string str2(keys.size()* sizeof(unsigned long int),'\0'); 
std :: copy(reinterpret_cast< unsigned char *>(& * begin(keys)),reinterpret_cast< unsigned char *>(& * end(keys)),str2.begin());
str2.resize(str.size());

如果要以相反的顺序存储字节,可以使用反向迭代器:

  std :: string str =Hello World! 
std :: vector< unsigned long int> keys(str.size()/ sizeof(unsigned long int)+1);

std :: copy(str.rbegin(),str.rbegin,reinterpret_cast< unsigned char *>(&(* keys.begin())));


I would like to convert a std::string into a std::vector<unsigned long int>, and be able to reverse it. Currently, I do this by this way :

std::string str = "Hello World !";
std::vector<unsigned long int> keys;
for(unsigned long int i = 0; i < str.length();)
{
  unsigned long int key = 0;
  for(unsigned long int j = 0; j < sizeof(unsigned long int) && i < str.length(); ++i,++j)
  {
    unsigned char uchar_value = static_cast<unsigned char>(str[i]);
    unsigned long int ulong_value = static_cast<unsigned long int>(uchar_value);
    key |= ulong_value << (j * CHAR_BIT);
  }
  keys.push_back(key);
}

Then, I have a similar procedure to reverse it. But it's quite ugly ! Is there a better/more elegant way to do it ? Maybe directly with memcpy ? But I don't see how.

解决方案

What about:

std::string str = "Hello World !";
std::vector<unsigned long int> keys(str.size() / sizeof(unsigned long int)+1);

std::copy(begin(str), end(str), reinterpret_cast<unsigned char*>(&(*keys.begin())));

And the reverse:

std::string str2(keys.size()*sizeof(unsigned long int), '\0');
std::copy(reinterpret_cast<unsigned char*>(&*begin(keys)), reinterpret_cast<unsigned char*>(&*end(keys)), str2.begin());
str2.resize(str.size());

And if you want to store the bytes in reverse order, you can use reverse iterators:

std::string str = "Hello World !";
std::vector<unsigned long int> keys(str.size() / sizeof(unsigned long int)+1);

std::copy(str.rbegin(), str.rbegin, reinterpret_cast<unsigned char*>(&(*keys.begin())));  

这篇关于将字符串存储在unsigned long int的向量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆