C++ 在C++;
现在我有了这个代码,它以用户输入确定的增量生成随机字母C++ 在C++;,c++,arrays,string,variables,chars,C++,Arrays,String,Variables,Chars,现在我有了这个代码,它以用户输入确定的增量生成随机字母 #include <iostream> #include <string> #include <cstdlib> using namespace std; int sLength = 0; static const char alphanum[] = "0123456789" "!@#$%^&*" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqr
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int sLength = 0;
static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int stringLength = sizeof(alphanum) - 1;
char genRandom()
{
return alphanum[rand() % stringLength];
}
int main()
{
cout << "What is the length of the string you wish to match?" << endl;
cin >> sLength;
while(true)
{
for (int x = 0; x < sLength; x++)
{
cout << genRandom();
}
cout << endl;
}
}
#包括
#包括
#包括
使用名称空间std;
int sLength=0;
静态常量字符alphanum[]=
"0123456789"
"!@#$%^&*"
“ABCDEFGHIJKLMNOPQRSTUVWXYZ”
“abcdefghijklmnopqrstuvwxyz”;
int stringLength=sizeof(alphanum)-1;
char genRandom()
{
返回alphanum[rand()%stringLength];
}
int main()
{
coutslength;
while(true)
{
对于(int x=0;x
string s(sLength, ' ');
在while(true)
之前,更改
cout << genRandom();
在循环中,删除cout,只需添加
string s(sLength, ' ');
在while(true)
之前,更改
cout << genRandom();
在你的循环中,删除cout那么,这个怎么样
std::string s;
for (int x = 0; x < sLength; x++)
{
s.push_back(genRandom());
}
std::字符串s;
对于(int x=0;x
这个怎么样
std::string s;
for (int x = 0; x < sLength; x++)
{
s.push_back(genRandom());
}
std::字符串s;
对于(int x=0;x
#包括
#包括
// ...
int main()
{
srand(time(0));//别忘了我
while(true){
coutslength;
字符串r(长度为“”);
生成(r.begin(),r.end(),genRandom);
不能包含
#包括
// ...
int main()
{
srand(time(0));//别忘了我
while(true){
coutslength;
字符串r(长度为“”);
生成(r.begin(),r.end(),genRandom);
除了字符串s(SLENGHT)之外,这行得通吗?它会弹出一个错误。从int到const char的转换无效。@西斯托伦:我假设它的工作方式类似于向量(可以这样构造)。我会修复它。最好是做s.reserve(SLENGHT)-这样你就不必写每个位置两次了。@kotlinski:我写元素时使用的是[]
而不是向后推
,因此我需要调整大小
而不是保留
;我认为调整大小
也会写所有元素。这可以在字符串s(sLength)之外工作;它弹出一个错误。从int到const char的转换无效。@Cistoran:我假设它的工作方式类似于vector
(可以这样构造)。我会解决它。最好是做s.reserve(sLength)-这样你就不必每一个位置都写两次。@kotlinski:我正在使用[]
而不是向后推
,因此我需要调整大小
而不是保留
;我认为调整大小
也会写入所有元素。