C++ 从大于n的数组中删除所有元素
我是编程新手。有些事情给我编写代码带来了麻烦。假设我有一个数组C++ 从大于n的数组中删除所有元素,c++,arrays,remove-if,C++,Arrays,Remove If,我是编程新手。有些事情给我编写代码带来了麻烦。假设我有一个数组 int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4}; 我想删除所有大于9的元素。我怎样才能做到这一点呢?如果使用vector,就可以做到这一点。首先用数组初始化向量。然后使用remove_if()函数。希望这会有所帮助 #include <algorithm> #include <vector> int main() { i
int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
我想删除所有大于9的元素。我怎样才能做到这一点呢?如果使用vector,就可以做到这一点。首先用数组初始化向量。然后使用remove_if()函数。希望这会有所帮助
#include <algorithm>
#include <vector>
int main()
{
int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
vector<int> V(Array, Array+20);
vector<int> :: iterator it;
it = remove_if(V.begin(), V.end(), bind2nd(greater<int>(), 9));
V.erase (it, V.end()); // This is your required vector if you wish to use vector
}
#包括
#包括
int main()
{
int数组[]={3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
向量V(数组,数组+20);
向量::迭代器;
它=如果(V.begin(),V.end(),bind2nd(greater(),9)),则删除_;
V.erase(it,V.end());//如果要使用向量,这是您所需的向量
}
无法从数组中删除项,因为它们的大小是固定的
如果使用了std::vector
,则解决方案如下所示:
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
std::vector<int> Array = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
Array.erase(remove_if(Array.begin(), Array.end(), [](int n) { return n > 9; }),
Array.end());
copy(Array.begin(), Array.end(), ostream_iterator<int>(cout, " "));
}
上面的命令会将“已擦除”项移到数组的末尾,并用-1标记它们
实例:
请注意STL算法函数的两个示例中的用法。数组的第二个示例使用相同的
remove\u if
算法函数。remove\u if
返回“已擦除”数据的开头,正如remove\u if
没有实际删除,而是将数据移动到序列的末尾。我尝试不使用向量的交换概念int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
int n;
int arr_len = sizeof(Array)/sizeof(int);
void print_array_value() {
int i;
cout << "\n";
for (i = 0; i < arr_len; i++) {
cout << Array[i] << ", ";
}
cout << " : " << arr_len << "\n";
}
void swap_array_value(int start) {
int i;
for ( ; (start+1) < arr_len; start++) {
Array[start] = Array[start+1];
}
}
void remove_array_value() {
int i;
for (i = 0; i < arr_len; i++) {
if (Array[i] > n) {
swap_array_value(i);
arr_len--;
i--;
}
}
}
void main () {
clrscr();
cout << "Enter the N value : ";
cin >> n;
print_array_value();
remove_array_value();
print_array_value();
cout << "Array Length : " << arr_len;
getch();
}
int数组[]={3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
int n;
int arr_len=sizeof(数组)/sizeof(int);
无效打印\数组\值(){
int i;
您不能从数组中删除元素。您最好将它们设置为某个特殊值。由于无法从数组中删除项,因此您在编码时遇到问题。可能是@trejder的重复项,谢谢。但我想在条件中删除元素如果无法从数组中删除元素,那么我将尝试使用此..t谢谢。为什么intk
?这是不必要的。V.erase(it,V.end());
@PaulMcKenzie,Oh..tnx。在C++11中,您可以使用std::end(Array)
而不是Array+aSize
(避免容易出错的sizeof
)。为了对称,您也可以使用std::begin()
。
int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
int n;
int arr_len = sizeof(Array)/sizeof(int);
void print_array_value() {
int i;
cout << "\n";
for (i = 0; i < arr_len; i++) {
cout << Array[i] << ", ";
}
cout << " : " << arr_len << "\n";
}
void swap_array_value(int start) {
int i;
for ( ; (start+1) < arr_len; start++) {
Array[start] = Array[start+1];
}
}
void remove_array_value() {
int i;
for (i = 0; i < arr_len; i++) {
if (Array[i] > n) {
swap_array_value(i);
arr_len--;
i--;
}
}
}
void main () {
clrscr();
cout << "Enter the N value : ";
cin >> n;
print_array_value();
remove_array_value();
print_array_value();
cout << "Array Length : " << arr_len;
getch();
}