我正在嘗試撰寫一個函式,通過洗掉偶數和復制奇數來修改陣列串列。我的代碼在洗掉偶數方面效果很好。但是,它只復制了一些奇數。
例如。我的串列包含:12 11 13 14 15 13 10 L.duplicateORremove() 之后的串列 L 應包含:11 11 13 13 15 15 13 13
但這就是我得到的:11 13 13 15 13 13
我需要一些幫助來弄清楚為什么我會得到這個結果。
到目前為止,這是我的代碼:
template <class Type>
void arrayListType <Type>::duplicateORremove()
{
for(int i=0; i<length; i )
{
if(list[i]%2==0)
{
for (int j = i; j < length - 1; j )
list[j] = list[j 1];
length--;
}
else
{
for (int j = length; j > i; j--)
list[j] = list[j - 1];
list[i 1] = list[i];
i ;
length ;
}
}
}
這就是我的類定義和主要的樣子:
template < class Type >
class arrayListType {
public:
arrayListType(int size = 100);
~arrayListType();
void insertEnd(const Type & insertItem);
void print() const;
void duplicateORremove();
protected:
Type * list; //array to hold the list elements
int length; //stores length of list
int maxSize; //stores maximum size of the list
};
template < class Type >
arrayListType < Type > ::arrayListType(int size) {
if (size < 0) {
cerr << "The array size must be positive. Creating " <<
"an array of size 100. " << endl;
maxSize = 100;
} else
maxSize = size;
length = 0;
list = new Type[maxSize];
assert(list != NULL);
}
template < class Type >
arrayListType < Type > ::~arrayListType() {
delete[] list;
}
template < class Type >
void arrayListType < Type > ::insertEnd(const Type & insertItem) {
if (length >= maxSize)
cerr<<"Cannot insert in a full list" << endl;
else {
list[length] = insertItem;
length ;
}
}
template < class Type >
void arrayListType < Type > ::print() const {
for (int i = 0; i < length; i )
cout << list[i] << " ";
cout << endl;
}
int main()
{
arrayListType <int> L;
L.insertEnd(12);
L.insertEnd(11);
L.insertEnd(13);
L.insertEnd(14);
L.insertEnd(15);
L.insertEnd(13);
L.insertEnd(10);
cout << "List L before L.duplicateORremove() contains:" << endl;
L.print();
L.duplicateORremove();
cout << "List L after L.duplicateORremove() contains:" << endl;
L.print();
uj5u.com熱心網友回復:
#include <iostream>
using namespace std;
void duplicateORremove(int list[], int length) {
for(int i=0; i<length; i )
{
if(list[i]%2==0)
{
for (int j = i; j < length - 1; j )
list[j] = list[j 1];
length--;
i-=1;
}
else
{
for (int j = length; j > i; j--)
list[j] = list[j - 1];
list[i 1] = list[i];
i ;
length ;
}
}
for (int i=0; i<length; i ) {
cout << list[i];
cout << " ";
}
}
int main()
{
int arrlist[] = {12, 11, 13, 14, 15, 13, 10};
duplicateORremove(arrlist, 7);
return 0;
}
當找到偶數時,您不會重置“i”值以從先前的狀態遍歷。由于在回圈時會跳過偶數的奇數,因此您最終會得到一個結果中缺少一些值的結果。缺失值的計數將等于輸入中偶數的計數。
只需添加該重置條件即可。
運行以上代碼的鏈接:在線cpp編譯器
uj5u.com熱心網友回復:
這是使用Range-v3庫的解決方案。
- 您可以對原始范圍的視圖進行操作。
- 洗掉偶數元素。
- 復制剩余串列的每個元素(奇數)。
- 然后將視圖轉換回向量并回傳。
[演示]
#include <fmt/ranges.h>
#include <range/v3/all.hpp>
template <typename Range>
auto duplicate_or_remove(Range&& r) {
using namespace ranges::v3;
auto even = [](int i) { return 0 == i % 2; };
return r
| views::remove_if(even)
| views::for_each([](auto e){ return yield_from(views::repeat_n(e, 2)); })
| to_vector;
}
int main() {
std::vector<int> v{12, 11, 13, 14, 15, 13, 10};
fmt::print("{}", duplicate_or_remove(v));
}
// Outputs:
//
// [11, 11, 13, 13, 15, 15, 13, 13]
回到你的代碼,注意你不能簡單地處理你的輸入串列。如果串列只包含奇數會發生什么?對于第一個元素,您將寫入超出為串列保留的記憶體。相反,如果您需要堅持使用陣列,您可以做的是:
- 計算輸入串列中的奇數元素。
- 創建一個新陣列,其大小是輸入串列中奇數元素數量的兩倍。
- 遍歷輸入串列,對于每個奇數元素,將其兩次添加到新創建的陣列中。
- 回傳由新陣列及其大小組成的對。
- 如果您傳遞的是陣列而不是指標,下面的代碼還使用了一個很好的技巧來避免將陣列的長度傳遞給函式。
[演示]
#include <iostream> // cout
#include <utility> // pair
auto is_odd = [](int i){ return (i % 2) == 1; };
template <size_t length>
size_t count_odd_elements(int (&list)[length]) {
size_t ret{};
for (size_t i{0}; i < length; i) {
if (is_odd(list[i])) {
ret ;
}
}
return ret;
}
template <size_t length>
auto duplicate_or_remove(int (&list)[length]) {
size_t odd_elements{ count_odd_elements(list) };
size_t ret_size{odd_elements * 2};
int* ret = new int[ret_size];
size_t head{};
for (size_t i{0}; i < length; i) {
int n{ list[i] };
if (is_odd(n)) {
ret[head ] = n;
ret[head ] = n;
}
}
return std::pair<int*, size_t>{ret, ret_size};
}
void print_list(int list[], size_t length) {
std::cout << "[";
for (size_t i{0}; i < length; i) {
std::cout << ((i == 0) ? "" : ", ") << list[i];
}
std::cout << "]";
}
int main() {
int arrlist[] = {12, 11, 13, 14, 15, 13, 10};
auto [result, result_size] = duplicate_or_remove(arrlist);
print_list(result, result_size);
delete[] result;
}
// Outputs:
//
// [11, 11, 13, 13, 15, 15, 13, 13]
Again, using std::vector instead of arrays simplify things a lot:
[Demo]
#include <fmt/ranges.h>
#include <iostream> // cout
#include <vector>
auto is_odd = [](int i){ return (i % 2) == 1; };
auto duplicate_or_remove(const std::vector<int>& list) {
std::vector<int> ret{};
for (auto i : list) {
if (is_odd(i)) {
ret.push_back(i);
ret.push_back(i);
}
}
return ret;
}
int main() {
std::vector<int> v{12, 11, 13, 14, 15, 13, 10};
auto result{ duplicate_or_remove(v) };
fmt::print("{}", result);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/435779.html
上一篇:繪制Mandelbrot集
