C++ 如何清除数组中的元素?
我想清除数组中的所有元素,但我不知道如何自动清除它。它有什么功能吗?就像列表的clear()一样C++ 如何清除数组中的元素?,c++,C++,我想清除数组中的所有元素,但我不知道如何自动清除它。它有什么功能吗?就像列表的clear()一样 int array[5] = {2,5,4,8,6}; 然后我想清除所有内容并添加一组新值您的问题无效,因为无法清除数组。数组有固定的大小,并且总是有一些值在其中 如果要重新使用数组,只需覆盖现有值 也许考虑使用 STD::向量< /代码>。使用clear()函数,可以清除std::vector中的所有值 了解std::vector您的问题无效,因为您无法清除数组。数组有固定的大小,并且总是有一些
int array[5] = {2,5,4,8,6};
然后我想清除所有内容并添加一组新值您的问题无效,因为无法
清除数组。数组有固定的大小,并且总是有一些值在其中
如果要重新使用数组,只需覆盖现有值
也许考虑使用<代码> STD::向量< /代码>。使用
clear()
函数,可以清除std::vector
中的所有值
了解std::vector
您的问题无效,因为您无法清除数组。数组有固定的大小,并且总是有一些值在其中
如果要重新使用数组,只需覆盖现有值
也许考虑使用<代码> STD::向量< /代码>。使用
clear()
函数,可以清除std::vector
中的所有值
了解std::vector
清除数组意味着将所有值设置为T(),对于基本算术类型的数组,这相当于将所有元素设置为零
你可以用几种方法来做。第一个是使用header
中声明的标准C函数std::memset
。比如说
#include <iostream>
#include <cstring>
int main()
{
int array[] = { 2, 5, 4, 8, 6 };
const size_t N = sizeof( array ) / sizeof( *array );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
std::memset( array, 0, N * sizeof( int ) );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <algorithm>
int main()
{
int array[] = { 2, 5, 4, 8, 6 };
const size_t N = sizeof( array ) / sizeof( *array );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
std::fill( array, array + N, 0 );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <iomanip>
#include <vector>
int main()
{
std::vector<int> array = { 2, 5, 4, 8, 6 };
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
array.clear();
std::cout << "array is empty - " << std::boolalpha << array.empty() << std::endl;
return 0;
}
在这两种情况下,程序输出都是
2 5 4 8 6
0 0 0 0 0
2 5 4 8 6
array is empty - true
如果需要可变长度数组,请使用标准容器std::vector
比如说
#include <iostream>
#include <cstring>
int main()
{
int array[] = { 2, 5, 4, 8, 6 };
const size_t N = sizeof( array ) / sizeof( *array );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
std::memset( array, 0, N * sizeof( int ) );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <algorithm>
int main()
{
int array[] = { 2, 5, 4, 8, 6 };
const size_t N = sizeof( array ) / sizeof( *array );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
std::fill( array, array + N, 0 );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <iomanip>
#include <vector>
int main()
{
std::vector<int> array = { 2, 5, 4, 8, 6 };
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
array.clear();
std::cout << "array is empty - " << std::boolalpha << array.empty() << std::endl;
return 0;
}
而不是array.clear()代码>也可以使用数组。调整大小(0)代码>在程序中。清除数组意味着将所有值设置为T(),对于基本算术类型的数组,这相当于将所有元素设置为零
你可以用几种方法来做。第一个是使用header
中声明的标准C函数std::memset
。比如说
#include <iostream>
#include <cstring>
int main()
{
int array[] = { 2, 5, 4, 8, 6 };
const size_t N = sizeof( array ) / sizeof( *array );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
std::memset( array, 0, N * sizeof( int ) );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <algorithm>
int main()
{
int array[] = { 2, 5, 4, 8, 6 };
const size_t N = sizeof( array ) / sizeof( *array );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
std::fill( array, array + N, 0 );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <iomanip>
#include <vector>
int main()
{
std::vector<int> array = { 2, 5, 4, 8, 6 };
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
array.clear();
std::cout << "array is empty - " << std::boolalpha << array.empty() << std::endl;
return 0;
}
在这两种情况下,程序输出都是
2 5 4 8 6
0 0 0 0 0
2 5 4 8 6
array is empty - true
如果需要可变长度数组,请使用标准容器std::vector
比如说
#include <iostream>
#include <cstring>
int main()
{
int array[] = { 2, 5, 4, 8, 6 };
const size_t N = sizeof( array ) / sizeof( *array );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
std::memset( array, 0, N * sizeof( int ) );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <algorithm>
int main()
{
int array[] = { 2, 5, 4, 8, 6 };
const size_t N = sizeof( array ) / sizeof( *array );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
std::fill( array, array + N, 0 );
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <iomanip>
#include <vector>
int main()
{
std::vector<int> array = { 2, 5, 4, 8, 6 };
for ( int x : array ) std::cout << x << ' ';
std::cout << std::endl;
array.clear();
std::cout << "array is empty - " << std::boolalpha << array.empty() << std::endl;
return 0;
}
而不是array.clear()代码>也可以使用数组。调整大小(0)代码>在程序中。你说的清除是什么意思?无法从数组中删除元素,因此即使您不使用它,数组中仍有一些值。您说的“清除”是什么意思?数组有固定数量的元素,因此不能删除元素。只需将新值放在那里即可。不需要澄清。但是,如果您使用std::array
,那么您可能只需要分配{}
。如果您想要一个可以从中删除元素的数组,也许您应该改为使用。我的意思是,如果您只想将所有元素设置为0或其他值,那么就有std::fill。但是它不会调整数组的大小。你说的清除是什么意思?无法从数组中删除元素,因此即使您不使用它,数组中仍有一些值。您说的“清除”是什么意思?数组有固定数量的元素,因此不能删除元素。只需将新值放在那里即可。不需要澄清。但是,如果您使用std::array
,那么您可能只需要分配{}
。如果您想要一个可以从中删除元素的数组,也许您应该改为使用。我的意思是,如果您只想将所有元素设置为0或其他值,那么就有std::fill。但它不会调整数组的大小。