C STL libstd - yszheda/wiki GitHub Wiki
- http://en.cppreference.com/w/cpp/language/storage_duration
- http://en.cppreference.com/w/cpp/utility/program/exit
- http://en.cppreference.com/w/cpp/utility/program/atexit
- https://stackoverflow.com/questions/10702980/atexit-function
std::ifstream stream("mona-lisa.raw", std::ios::in | std::ios::binary);
std::vector<uint8_t> contents((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
- How to read a binary file into a vector of unsigned chars
- how to efficiently read a binary file into a vector C++
// ostream_iterator example
#include <iostream> // std::cout
#include <iterator> // std::ostream_iterator
#include <vector> // std::vector
#include <algorithm> // std::copy
int main () {
std::vector<int> myvector;
for (int i=1; i<10; ++i) myvector.push_back(i*10);
std::ostream_iterator<int> out_it (std::cout,", ");
std::copy ( myvector.begin(), myvector.end(), out_it );
return 0;
}
template<typename InputIt>
std::string join(InputIt begin,
InputIt end,
const std::string & separator =", ", // see 1.
const std::string & concluder ="") // see 1.
{
std::ostringstream ss;
using value_type = typename std::iterator_traits<InputIt>::value_type;
std::copy(begin, end, std::ostream_iterator<value_type>(ss, separator.c_str()));
ss << concluder;
return ss.str();
}
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <iostream>
std::string joint_strings(
const std::vector<std::string>& str_vector,
const char separator = ',',
const char concluder = ';')
{
#if 1
std::ostringstream ss;
auto sep = std::string(&separator);
std::copy(
str_vector.begin(),
str_vector.end(),
// std::ostream_iterator<std::string>(ss, &separator));
// std::ostream_iterator<std::string>(ss, ","));
std::ostream_iterator<std::string>(ss, sep.c_str()));
ss << concluder;
return ss.str();
#else
std::string result;
for (const auto& str : str_vector)
{
result += str + separator;
}
result += concluder;
return result;
#endif
}
int main()
{
std::vector<std::string> str_vector;
str_vector.push_back("GrpPowerOn");
str_vector.push_back("0");
for (auto i = 0; i < 10; i++)
{
auto str = joint_strings(str_vector);
std::cout << "size: " << str.size() << " string: " << str << std::endl;
}
return 0;
}
$ ./a.out
size: 16 string: GrpPowerOn,0,;
size: 14 string: GrpPowerOn,0,;
size: 14 string: GrpPowerOn,0,;
size: 14 string: GrpPowerOn,0,;
size: 14 string: GrpPowerOn,0,;
size: 14 string: GrpPowerOn,0,;
size: 14 string: GrpPowerOn,0,;
size: 14 string: GrpPowerOn,0,;
size: 14 string: GrpPowerOn,0,;
size: 14 string: GrpPowerOn,0,;
- How to implode a vector of strings into a string (the elegant way)
- std::vector to string with custom delimiter
- https://en.cppreference.com/w/cpp/io/basic_ios/rdbuf
- http://www.cplusplus.com/reference/ios/ios/rdbuf/
- http://www.cplusplus.com/reference/streambuf/streambuf/pubsetbuf/
- https://en.cppreference.com/w/cpp/io/basic_streambuf/pubsetbuf
- http://www.cplusplus.com/reference/streambuf/streambuf/sgetn/
- https://en.cppreference.com/w/cpp/io/basic_streambuf/sputn
- http://www.cplusplus.com/reference/ios/ios/tie/
- What is the meaning of writing cin.tie (false) in C++?
- https://en.cppreference.com/w/cpp/io/basic_istream
- Why do we need to tie std::cin and std::cout?
- http://www.cplusplus.com/reference/ios/ios_base/sync_with_stdio/
- https://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio
- cin.tie与sync_with_stdio加速输入输出
std::stringstream().swap(m); // swap m with a default constructed stringstream
// Workaround gcc bug of not supporting `stringstream` swap and move
m.str(std::string());
- std::stringstream.str() is always the same string being returned?
- What is wrong with stringstream str function?
- Discover the traps when using stringstream.str()
- [!] stringstream, string, and char* conversion confusion
- http://www.cplusplus.com/reference/cstdio/vsnprintf/
- http://www.cplusplus.com/reference/cstdio/snprintf/
- std::string formatting like sprintf
-
Optimization techniques used by std::regex_constants::optimize
-
Does the 'o' modifier for Perl regular expressions still provide any benefit?
std::array<std::array<int, 3>, 2> a2 { { { {1, 2, 3} }, { { 4, 5, 6} } } };
// ^ ^ ^ ^ ^ ^
// | | | | | |
// | +-|-+------------|-+
// +-|-+-|------------+---- C++ class braces
// | |
// +---+--- member C array braces
- Initializer-list for initializing 2D std::array member
- Initializer list on two dimensional std::array
- std::dynarray vs std::vector
- Any alternative to std::dynarray presently available?
- https://en.cppreference.com/w/cpp/numeric/valarray
- http://www.cplusplus.com/reference/valarray/valarray/
- Runtime-sized arrays with automatic storage duration (revision 4)
- http://en.cppreference.com/w/cpp/container/vector/shrink_to_fit
- https://stackoverflow.com/questions/10464992/c-delete-vector-objects-free-memory
- Is shrink_to_fit the proper way of reducing the capacity a
std::vector
to its size?
//vector<vector<int>> M;
//int m = number of rows, n = number of columns;
M.resize(m, vector<int>(n));
targetMap.insert(sourceMap.begin(), sourceMap.end());
- What is difference between const and non const key?
- https://stackoverflow.com/questions/6307321/does-it-make-sense-to-make-stdmap-key-const
struct EnumClassHash
{
template <typename T>
std::size_t operator()(T t) const
{
return static_cast<std::size_t>(t);
}
};
enum class MyEnum {};
std::unordered_map<MyEnum, int, EnumClassHash> myMap;
- Pointers as keys in map C++ STL
- How can I create my own comparator for a map?
- std::maps with user-defined types as key
-
https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
-
How can I change the variable to which a C++ reference refers?
- How to Modify a Key in a C++ Map or Set
- What is the fastest way to change a key of an element inside std::map
- https://en.cppreference.com/w/cpp/container/map/extract
- https://en.cppreference.com/w/cpp/container/multimap
- https://en.cppreference.com/w/cpp/container/multimap/equal_range
- Which One Is Better: Map of Vectors, or Multimap?
- Finding all values for a key in multimap using equals_range – Example
- which element will be returned from std::multimap::find?
- std::iterator is deprecated: Why, What It Was, and What to Use Instead
- How the STL inserter iterator really works
- Lower and Upper Bound Insert Iterators
- A smart iterator for inserting into a sorted container in C++
- A smart iterator for aggregating new elements with existing ones in a map or a set
- http://en.cppreference.com/w/cpp/algorithm/iota
- https://stackoverflow.com/questions/39923968/in-c-how-to-initialize-a-vector-with-values-1-2-n-without-a-loop?noredirect=1&lq=1
// numeric_limits example
#include <iostream> // std::cout
#include <limits> // std::numeric_limits
int main () {
std::cout << std::boolalpha;
std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';
std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';
std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';
std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';
std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';
return 0;
}
- http://www.cplusplus.com/reference/limits/numeric_limits/
- C++如何取得int型的最大最小值
- https://stackoverflow.com/questions/1732011/c-max-integer
- https://stackoverflow.com/questions/1855459/maximum-value-of-int
template<class T>
const T& clamp(const T& x, const T& upper, const T& lower) {
return min(upper, max(x, lower));
}
- http://en.cppreference.com/w/cpp/algorithm/clamp
- https://stackoverflow.com/questions/21656574/does-anyone-happen-to-know-why-there-is-no-clamp-function-in-math-h
- std::remove_cv, std::remove_const, std::remove_volatile
- https://stackoverflow.com/questions/15887144/stdremove-const-with-const-references
- http://www.cplusplus.com/reference/cmath/ceil/
- Does casting
std::floor()
andstd::ceil()
to integer type always give the correct result?
- https://en.cppreference.com/w/cpp/numeric/math/nearbyint
- https://en.cppreference.com/w/c/numeric/fenv/feround
- How to round half to even
#include <cfenv>
#include <cmath>
std::fesetround(FE_TONEAREST); // This is the default rounding mode for std::nearbyint
std::nearbyint(3.5f); // Result is 4
std::nearbyint(4.5f); // Result is 4
- How to convert wstring into string?
- How to initialize and print a std::wstring?
- https://en.cppreference.com/w/cpp/locale/setlocale
- http://en.cppreference.com/w/cpp/locale/locale
- http://en.cppreference.com/w/cpp/algorithm/copy
- https://stackoverflow.com/questions/7285952/efficiency-of-stdcopy-vs-memcpy
- https://stackoverflow.com/questions/4707012/is-it-better-to-use-stdmemcpy-or-stdcopy-in-terms-to-performance
- https://en.cppreference.com/w/cpp/string/byte/memcmp
- How to check whether all bytes in a memory block are zero
static int memvcmp(void *memory, unsigned char val, unsigned int size)
{
unsigned char *mm = (unsigned char*)memory;
return (*mm == val) && memcmp(mm, mm + 1, size - 1) == 0;
}
// assuming your vector is called v
iter_swap(v.begin() + position, v.begin() + nextPosition);
// position, nextPosition are the indices of the elements you want to swap
- http://en.cppreference.com/w/cpp/algorithm/min
- http://en.cppreference.com/w/cpp/algorithm/min_element
- Is it better to use std::memcpy() or std::copy() in terms to performance?
- http://www.enseignement.polytechnique.fr/informatique/INF478/docs/Cpp/en/cpp/algorithm/copy.html
-
How do I remove a C++ std: :function from a vector of std: :function within that same function?
-
Using std::function and std::bind to store callback and handle object deletion.
- c++11 Thread class how to use a class member function
- /usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void
- c++: error: no type named ‘type’ in ‘class std::result_of<void (*(std::unordered_map
- How can I use something like std::vectorstd::mutex?
- http://en.cppreference.com/w/cpp/thread/call_once
- How to make sure a function is only called once
- std::call_once vs std::mutex for thread-safe initialization
- std::once_flag and std::call_once implementation
- Threadsafe lazy initialization: static vs std::call_once vs double checked locking
- Initialize once_flag?
- http://en.cppreference.com/w/cpp/memory/unique_ptr
- C++ unique_ptr and map
- Why can I not push_back a unique_ptr into a vector?
- http://en.cppreference.com/w/cpp/utility/functional/ref
- http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
-
Measure time in Linux - time vs clock vs getrusage vs clock_gettime vs gettimeofday vs timespec_get?
-
Is there any std::chrono thread safety guaranty even with multicore context?
-
C++ How do I convert a std::chrono::time_point to long and back
-
How to get duration, as int milli's and float seconds from ?
#include <iostream>
#include <chrono>
#include <ctime>
int main()
{
auto start = std::chrono::system_clock::now();
// Some computation here
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout << "finished computation at " << std::ctime(&end_time)
<< "elapsed time: " << elapsed_seconds.count() << "s\n";
}
#include <iostream>
#include <chrono>
#include <iomanip>
using namespace std;
using namespace chrono;
// Prints UTC timestamp
void printTime() {
time_point<system_clock> now = system_clock::now();
time_t now_time = system_clock::to_time_t(now);
auto gmt_time = gmtime(&now_time);
auto timestamp = std::put_time(gmt_time, "%Y-%m-%d %H:%M:%S");
cout << timestamp << endl;
}
- https://en.cppreference.com/w/cpp/chrono/c/strftime
- https://en.cppreference.com/w/cpp/io/manip/put_time
- http://www.cplusplus.com/reference/ctime/gmtime/
- https://en.cppreference.com/w/cpp/chrono/c/gmtime
- http://en.cppreference.com/w/cpp/algorithm/partial_sort
- Use std::sort to find top N items in a std::vector
- http://en.cppreference.com/w/cpp/algorithm/remove
- http://www.cplusplus.com/reference/algorithm/remove_if/
- How to make std::vector from other vector with specific filter?
- Efficient way to filter out elements from std::vector
// Way #1
std::remove_copy_if(
all_items.begin(),
all_items.end(),
std::back_inserter(filter_items),
[&bad_ids](const mystruct& item) { return std::find(bad_ids.begin(), bad_ids.end(), item.id) != bad_ids.end(); });
// Way #2
all_items.erase(
std::remove_if(
all_items.begin(),
all_items.end(),
[&bad_ids](const mystruct& item) { return std::find(bad_ids.begin(), bad_ids.end(), item.id) != bad_ids.end(); }),
all_items.end());
- Filter out elements from std::vector
- Filter std::vector of std::string's
- C++ remove_if on a vector of objects
- https://en.cppreference.com/w/cpp/types/is_same
- https://stackoverflow.com/questions/874298/c-templates-that-accept-only-certain-types
- What is the use of value_type in STL containers?
- Declare template function to accept any container but only one contained type
c++filt -t