在C++中,切片通常是使用std::vector
或者std::array
來實現的。對于std::vector
,可以使用size()
方法來獲取切片的長度,使用capacity()
方法來獲取切片的容量。示例如下:
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
std::cout << "Length: " << numbers.size() << std::endl;
std::cout << "Capacity: " << numbers.capacity() << std::endl;
return 0;
}
對于std::array
,由于其固定大小,其長度即為其容量。示例如下:
#include <iostream>
#include <array>
int main() {
std::array<int, 5> numbers = {1, 2, 3, 4, 5};
std::cout << "Length: " << numbers.size() << std::endl;
std::cout << "Capacity: " << numbers.size() << std::endl;
return 0;
}