Arc*_*co3 3 c++ return function
我正在编写一个程序,该程序对从用户输入获取的字符串执行简单的旋转(类似于 rot13)。我的问题是,我想每次将字符串的 ASCII 值中的每个字符更改不同的量 - 因此我使用 for 循环来遍历字符串,并调用每次生成随机数的函数。但是,我希望能够返回这个数字,以便我可以在以后“解读”该字符串。显然,我还需要返回字符串。
这是我的代码:
int ranFunction()
{
int number = rand() % 31;
return number;
}
string rotFunction(string words)
{
int upper_A = 65;
int lower_z = 122;
int rand_int = 0;
for (int i = 0; i < words.length(); i++)
{
rand_int = ranFunction();
if (words[i] >= upper_A && words[i] <= lower_z) {
words[i] -= rand_int;
}
}
return words;
}
Run Code Online (Sandbox Code Playgroud)
我希望 rotFunction 返回单词和一个基于 rand_int 每次发生的整数。
请注意:我使用的数字 RE: ascii 值等现在完全是任意的,只是用于测试。
要返回两种不同的类型,请使用std::pair
std::pair<T1,T2> foo(){
return std::make_pair(v1,v2);
}
Run Code Online (Sandbox Code Playgroud)
例子:
std::pair<int,float> foo(){
return std::make_pair(5,0.5f);
}
Run Code Online (Sandbox Code Playgroud)
要返回两种以上不同类型,请使用std::tuple:
std::tuple<T1,T2,...,Tn> foo(){
return std::make_tuple(v1,v2,...,vn);
}
Run Code Online (Sandbox Code Playgroud)
例子:
std::tuple<int,float,std::string> foo(){
return std::make_tuple(5,0.5f,"sss");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12289 次 |
| 最近记录: |