我认为您在考虑这一点时过于认真:除非您需要保留重复值或模式的记录,否则无需任何临时人员或复制或移动数据。您可以通过在现有容器中直接轻松地工作来实现这一点。此外,当您发现自己开始使用带有嵌套 if 语句和另一个嵌套 for 循环的 for 循环时;这时你应该用一个带有几个 if 语句的 while 循环替换整个代码块。有时最好回到基础并做简单的逻辑流程。试试这个,让我知道你的想法。
#include <iostream>
int main() {
// Your Existing Array or Container
int numArray[] = {1,2,2,2,1,1,1,3,3,2,2};
// Number Of Elements in that container
unsigned numElements = sizeof(numArray) / sizeof(int);
// Initial Index Value For Our While Loop;
int index = 0;
// Loop From Initial Index To Last Element
while ( index < numElements ) {
// Check To See If Index Is Last Element If So Break From Loop
if ( index == numElements ) {
break;
}
// First Do Not Check To See If The Value At Index & Next Are Equal, Rather Check
// To See If They Are Not Equal. If They Are Not, Then Print The Value At That
// Index And A New Line, Otherwise Just Print The Value At That Index And A Space.
if ( numArray[index] != numArray[index+1] ) {
std::cout << "numArray[index] << std::endl;
} else {
std::cout << numArray[index] << " ";
}
// Increment Our Counter
index++;
}
// We Are Now Done; Not Necessary To Create Temporaries Or To Copy Or Move Data
// It Is A Little Easier To Read And Follow Logic Flow As Opposed To
// Having For Loop -> Nested If Stated -> Nested For Loop & More...
return 0;
}
您需要做的就是遵循这个 while 循环的模式或流程,它工作得很好。循环遍历容器的大小,首先检查退出条件,然后检查满足哪些条件才能分支到您需要执行的逻辑操作过程,最后增加您的计数器。这里唯一的主要区别是我使用的是 while 循环而不是 for 循环,并且我使用的是 std::cout 而不是 printf()。
现在,如果您需要对它们进行分组,可以将其添加到同一个 while 循环中。
#include <iostream>
#include <vector>
int main() {
int numArray[] = {1,2,2,2,1,1,1,3,3,2,2};
unsigned numElements = sizeof(numArray) / sizeof (int);
std::vector<int> group;
std::vector<std::vector<int>> groups;
int index = 0;
while ( index < numElements ) {
if ( index == numElements ) {
break;
}
if ( numArray[index] != numArray[index+1] ) {
std::cout << numArray[index] << std::endl;
group.push_back( numArray[index] ); // Add To Group
groups.push_back( group ); // Add To Groups
group.clear(); // Clear Group For Change In Value
//std::cout << "\n";
} else {
std::cout << numArray[index] << " ";
group.push_back( numArray[index] ); // Just Add To Group Do Not Clear
}
index++;
}
std::cout << std::endl;
// Test Groups;
for ( unsigned i = 0; i < groups.size(); i++ ) {
for ( unsigned j = 0; j < groups[i].size(); j++ ) {
std::cout << groups[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
} // main
现在,如果您想让它更加模块化、可重用和通用,您可以将其放入模板函数中:
#include <iostream>
#include <vector>
template<typename T>
void groupObjects( unsigned startIndex, unsigned size, T* objectArray, std::vector<std::vector<T>>& groups ) {
std::vector<T> group;
while ( startIndex < size ) {
if ( startIndex == size ) {
break;
}
if ( objectArray[startIndex] != objectArray[startIndex+1] ) {
std::cout << objectArray[startIndex] << std::endl;
group.push_back( objectArray[startIndex] );
groups.push_back( group );
group.clear();
} else {
std::cout << objectArray[startIndex] << " ";
group.push_back( objectArray[startIndex] );
}
startIndex++;
}
}
int main() {
int numArray[] = {1,2,2,2,1,1,1,3,3,2,2};
unsigned numElements = sizeof(numArray) / sizeof (int);
std::vector<std::vector<int>> results;
groupObjects<int>( 0, numElements, numArray, results );
std::cout << std::endl;
for ( unsigned i = 0; i < results.size(); i++ ) {
for ( unsigned j = 0; j < results[i].size(); j++ ){
std::cout << results[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}