gpt4 book ai didi

c++ - 使用一个数组以相反的顺序制作另一个数组

转载 作者:行者123 更新时间:2023-11-27 22:53:40 25 4
gpt4 key购买 nike

这里真的需要一些帮助。我的 testList 是字母表的数组 [26]。我想使用循环为 newList 执行相反的顺序。

当我在循环内测试 newList 的输出时它有效,但如果我在循环外测试它则不起作用。如果有人能帮助我,我将不胜感激!

提前致谢。

 void CText::createList(){
int i;
int j;

for (i = 0; i < 26; i++) {
counter = 0;
j = 25;
newList[j] = textList[i];
j--;
//cout << newList[j] << endl;

}

cout << newList[0] << endl;
}

最佳答案

void CText::createList(){
int i;
for (i = 0; i < 25; i++) {
newList[25-1-i] = textList[i];
}
cout << newList[0] << endl;
}

但是,最好使用 vector 并针对您的问题使用反向迭代器。示例:

#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> normal_vector{0,1,2,3,4,5,6,7,8,9};
std::vector<int> reverse_vector(normal_vector.rbegin(),normal_vector.rend());

for(auto const& item:normal_vector){
std::cout << item << "\t";
}
std::cout << std::endl;
for(auto const& item:reverse_vector){
std::cout << item << "\t";
}
}

Live Demo

关于c++ - 使用一个数组以相反的顺序制作另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35170771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com