c++產生驗證碼字符串


//
// Created by lk on 18-10-14.
//
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

// 實例:產生一個長度為len的字符串驗證碼,並且由16個字母和0到9的數字組成
void RandStr(vector<char>&vec, const int len)
{
    srand((int)time(0));  // 產生隨機種子  把0換成NULL也行
    char temp;
    for (int i = 0; i < len; i++)
    {
        switch (rand()%3)
        {
            case 0:
                temp= rand()%10 + '0';   
                break;

            case 1:
                temp = rand()%26 + 'a';
                break;

            case 2:
                temp = rand()%26 + 'A';
                break;
        }
        vec.push_back(temp);
    }
}
int main()
{
    vector<char>vec;
    int len = 4;
    RandStr(vec, len);
    for (int i = 0; i < len; i++)
    {
        cout << vec[i];
    }
    
    return 0;
}

 注意switch里面不能有臨時變量


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM