Home > Article > Backend Development > How to convert C++ STL container to other types?
In C++, methods for converting an STL container to other types include copying or converting elements into another container using standard algorithms such as std::copy. Use a container adapter (such as std::list) to wrap the container to obtain a different interface. Write custom functions to perform complex transformations or specific operations.
The Standard Template Library (STL) in C++ provides a series of Powerful containers that provide mechanisms for efficiently storing and accessing data. Sometimes, you may need to convert these containers to other types for further processing or integration into other systems. This article describes several ways to convert STL containers to other types in C++.
The C++ standard library provides algorithms such as std::copy
and std::transform
, which can be used to copy or transform elements in a container into another container.
// 将 vector<int> 转换为 deque<int> #include <vector> #include <deque> #include <algorithm> int main() { std::vector<int> myVector{1, 2, 3, 4, 5}; std::deque<int> myDeque; std::copy(myVector.begin(), myVector.end(), std::back_inserter(myDeque)); return 0; }
A container adapter is a special mechanism in C++ that allows you to use the interface of one type of container to access another type of container. std::list
The container adapter can wrap any container into a doubly linked list.
// 将 vector<int> 转换为 list<int> #include <vector> #include <list> #include <algorithm> int main() { std::vector<int> myVector{1, 2, 3, 4, 5}; std::list<int> myList(myVector.begin(), myVector.end()); return 0; }
You can write your own function to convert containers. This method is useful for complex transformations or performing specific operations.
// 将 vector<int> 转换为 map<int, int>,其中键为元素本身,值为 0 #include <vector> #include <map> #include <functional> int main() { std::vector<int> myVector{1, 2, 3, 4, 5}; std::map<int, int> myMap; std::transform(myVector.begin(), myVector.end(), std::inserter(myMap, myMap.end()), std::bind(std::make_pair<int, int>, std::placeholders::_1, 0)); return 0; }
Suppose you have a std::vector7c07761aae243fb25620b0fe3de0dd1a
, which contains a set of file paths. You need to convert this vector to an unordered set of type std::unordered_set7c07761aae243fb25620b0fe3de0dd1a
to quickly check the uniqueness of the file.
You can use the following code:
#include <vector> #include <unordered_set> #include <algorithm> int main() { std::vector<std::string> filePaths{"file1.txt", "file2.txt", "file3.txt", "file1.txt"}; std::unordered_set<std::string> uniqueFilePaths; std::copy(filePaths.begin(), filePaths.end(), std::inserter(uniqueFilePaths, uniqueFilePaths.end())); return 0; }
This code will iterate over a vector of file paths and insert them into an unordered set using the std::copy
algorithm. std::inserter
is a special function object that allows you to insert elements into a container.
The above is the detailed content of How to convert C++ STL container to other types?. For more information, please follow other related articles on the PHP Chinese website!