本文整理汇总了C++中typenamestd::vector::reserve方法的典型用法代码示例。如果您正苦于以下问题:C++ vector::reserve方法的具体用法?C++ vector::reserve怎么用?C++ vector::reserve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类typenamestd::vector
的用法示例。
在下文中一共展示了vector::reserve方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getCallbacks
std::vector<T> getCallbacks() {
typename std::vector<T> result;
result.reserve(functionList.size() - invalidIndexCount);
for (typename TypedFunctionPointerWrapperRefVector<T>::type::iterator it = functionList.begin(); it != functionList.end(); it++) {
if (*it) {
result.push_back((*it)->callback);
}
}
return result;
}
示例2: encodeImageToPNG
template<typename T> void
encodeImageToPNG (typename std::vector<T>& image_arg,
size_t width_arg,
size_t height_arg,
int image_format_arg,
typename std::vector<uint8_t>& pngData_arg,
int png_level_arg)
{
png_structp png_ptr;
png_infop info_ptr;
volatile int channels;
if (image_arg.size () ==0)
return;
// Get amount of channels
switch (image_format_arg)
{
case PNG_COLOR_TYPE_GRAY:
channels = 1;
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
channels = 2;
break;
case PNG_COLOR_TYPE_RGB:
channels = 3;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
channels = 4;
break;
default:
channels = 0;
break;
}
// Ensure valid input array
assert (image_arg.size () == width_arg*height_arg*channels);
// Initialize write structure
png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
assert (png_ptr && "creating png_create_write_structpng_create_write_struct failed");
// Initialize info structure
info_ptr = png_create_info_struct (png_ptr);
assert (info_ptr && "Could not allocate info struct");
// Setup Exception handling
setjmp(png_jmpbuf(png_ptr));
// reserve memory for output data (300kB)
pngData_arg.clear ();
pngData_arg.reserve (300 * 1024);
// Define I/O methods
png_set_write_fn (png_ptr, reinterpret_cast<void*> (&pngData_arg),
user_write_data, user_flush_data);
// Define zlib compression level
if (png_level_arg >= 0)
{
png_set_compression_level (png_ptr, png_level_arg);
}
else
{
png_set_compression_level (png_ptr, Z_DEFAULT_COMPRESSION);
}
// Write header
png_set_IHDR (png_ptr, info_ptr, width_arg, height_arg, sizeof(T) * 8,
image_format_arg, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info (png_ptr, info_ptr);
// Write image data
size_t y;
for (y = 0; y < height_arg; y++)
{
png_write_row (png_ptr, reinterpret_cast<png_bytep> (&image_arg[y * width_arg * channels]));
}
// End write
png_write_end (png_ptr, 0);
if (info_ptr)
png_free_data (png_ptr, info_ptr, PNG_FREE_ALL, -1);
if (png_ptr)
png_destroy_write_struct (&png_ptr, 0);
}