如何在C ++中生成一组唯一的哈希字符串? [英] How to generate a set of unique hash strings in C++?

查看:159
本文介绍了如何在C ++中生成一组唯一的哈希字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码产生26 * 26 * 26 * 26 hasname(使用az的组合)或者你可以说随机名称,我想分配给一个结构成员。现在当我分配,首先分配结构成员足够的内存然后使用strcpy,只有这个代码生成的最后一个hashname被传递给结构,即zzzz(它是最后一个hashname)。我可以这样做,hashname从开始分配。

This code produce 26*26*26*26 hasname (using combinations of a-z)or you can say random names which i want to assign to a structure member.Now when i am assigning that by first allocating that structure member sufficient memory and then using strcpy, only last hashname generated by this code is being passed to the structure i.e zzzz(it is the last hashname).What can i do so that hashname is assigned from the starting.

vcd_xyz[4] = '\0';
int  count = 0;
for(int i=0;i<26;i++)
{
    vcd_xyz[0] = 'a'+i;
    for(int j=0;j<26;j++)
    {
        vcd_xyz[1] = 'a'+j;
        for(int k = 0;k<26;k++)
        {
            vcd_xyz[2] = 'a' + k;
            for(int l=0;l<26;l++)
            {
                vcd_xyz[3] = 'a' +l;
                count ++;
                printf("%s\n",vcd_xyz);
            }
        }
    }
}



我使用

So i am using

sss->Variables[0].hashname = (char*)calloc(strlen((char*)vcd_xyz)+1,sizeof(char));
strcpy(sss->Variables[0].hashname,(char*)vcd_xyz);

复制生成的hasname,但它正在复制生成的最后一个hashname,所以我使用 tr0-> Variables [0] .hashname =(char *)calloc(strlen((char *)vcd_xyz)+ 1,sizeof(char)
strcpy(tr0-> Variables [0] .hashname,(char *)vcd_xyz);
只有zzzz get printed.what我做错了什么,以顺序方式分配。

to copy the hasname produced but it is copying the last hashname produced, so where ever i am using tr0->Variables[0].hashname = (char*)calloc(strlen((char*)vcd_xyz)+1,sizeof(char)); strcpy(tr0->Variables[0].hashname,(char*)vcd_xyz); only zzzz get printed.what i am doing wrong or what should i do so that hashname are assigned in a sequential manner.

推荐答案

首先你需要意识到 char vcd_xyz [4] 是一个4个字符的数组,这意味着你可以在那里放3个字符+结束符'\0'。如果您处理此数组,因为它是null终止的,当它不是,它会导致 未定义的行为

At first you need to realize that char vcd_xyz[4] is an array of 4 characters, meaning that you can put there 3 characters + terminating character '\0'. If you treat this array as it is null-terminated when it is not, it results in undefined behavior.

你的代码实际上做的是它遍历所有可能的4个字母长字符串的组合,从aaaazzzz,留下用最后一个组合填充的 vcd_xyz 数组(即zzzz)。

What your code actually does is that it iterates through all possible combinations of 4 letters long strings, from "aaaa" to "zzzz", leaving the vcd_xyz array filled with the last combination (i.e. "zzzz").

如果你想生成随机4个字母的长字符串,可以使用C风格的函数:

If you want to generate random 4-letters long string, here's the C-style function you might use:

int irand(int min, int max) {
    return ((double)rand() / ((double)RAND_MAX + 1.0)) * (max - min + 1) + min;
}

它会从< min; max> ; interval,它可以这样使用:

it generates random number from <min;max> interval and it can be used like this:

std::string generateHashTag() {
    char str[5];
    for (int i = 0; i < 4; ++i)
        str[i] = irand('a', 'z');
    str[4] = '\0';
    return std::string(str);
}

但是,如果您想生成 独特的4个字母长的哈希标记,您将需要更复杂的解决方案。在C ++中,你可以很容易地在一个循环中生成这些字符串,将它们放入一个 std :: set 容器,直到你有足够的,或者你可以生成更多的唯一组合字符串,将所有这些放入 std :: vector ,随机选择并选择 N ,例如: p>

But in case you want to generate a set of unique 4-letters long hash tags, you will need more complex solution. In C++ you might easily generate these strings in a loop that will put them into an std::set container till you have enough of them or you might generate more unique combinations of this string, put all of these into an std::vector, shuffle it and pick first N, e.g.:

const size_t N = 5;
std::set<std::string> myHashTags;

srand(time(0));
while (myHashTags.size() < N)
    myHashTags.insert(generateHashTag());

for (std::set<std::string>::iterator i = myHashTags.begin();
     i != myHashTags.end(); ++i)
     std::cout << *i << ' ';

输出 kemy snwv vnmi wfmm wqeg 。完整示例此处

这篇关于如何在C ++中生成一组唯一的哈希字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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