Convert String to Integer Vector in C++ Last Updated : 06 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Strings are sometimes used to represent the numerical data which needs to be converted back to integer, but sometimes we may need to convert it to vector of integers instead due to some problems such as too large integer. In this article, we will learn different way to convert a string to integer vector in C++The simplest method to convert a string to an integer vector is by using transform() function to convert each character to integer and store it in the vector. Let’s take a look at an example: C++ #include <bits/stdc++.h> using namespace std; int main() { string s = "12345"; vector<int> v; // Convert string to integer vector using transform transform(s.begin(), s.end(), back_inserter(v), [](char c) { return c - '0'; }); for (int i : v) cout << i << " "; return 0; } Output1 2 3 4 5 Explanation: The transform() function applied the function that converts the numeric character to its corresponding digit to each character of the string s and stores it in the vector v.There are a few more case specific methods available in C++ to convert the string to integer vector. They are as follows:Using Loop and Vector push_back()Iterate the string while converting each character to its corresponding numeric value using ASCII codes and store it in the vector using vector push_back() method. C++ #include <bits/stdc++.h> using namespace std; int main() { string s = "12345"; vector<int> v; // Convert each character in the string to an integer for (char c : s) v.push_back(c - '0'); for (int i : v) cout << i << " "; return 0; } Output1 2 3 4 5 Comment More infoAdvertise with us Next Article How to Iterate 2D Vector in C++? H harsh464565 Follow Improve Article Tags : Technical Scripter C++ Programs C++ Technical Scripter 2022 STL cpp-vector C Conversion Programs +3 More Practice Tags : CPPSTL Similar Reads How to Convert Vector to Multiset in C++? In C++, vector is a dynamic array that allows us to store multiple elements sequentially and multiset is a container that allows us to store multiple elements having duplicate values in some sorted order. In this article, we will learn how to convert a vector to multiset in C++. For Example Input: m 2 min read How to convert a Vector to Set in C++ This article shows how to convert a vector to a set in C++. Example: Input: vector = {1, 2, 3, 1, 1} Output: set = {1, 2, 3} Input: vector = {1, 2, 3, 3, 5}Output: set = {1, 2, 3, 5} Below are the various ways to do the required conversion: Method 1: Naive SolutionGet the vector to be converted.Crea 4 min read How to Iterate Through a Vector in C++? Iterating or traversing a vector means accessing each element of the vector sequentially. In this article, we will learn different methods to iterate over a vector in C++.The simplest way to iterate a vector is by using a range based for loop. Let's take a look at a simple example that iterate the v 3 min read How to Iterate 2D Vector in C++? Iterating or traversing a 2D vector means accessing each element of the 2D vector sequentially. In this article, we will explore different methods to iterate over a 2D vector in C++.The simplest way to iterate a 2D vector is by using a range-based for loop. Let's take a look at a simple example that 3 min read How to Insert into Vector Using Iterator in C++? In C++, a vector is a dynamic array that can grow and shrink in size as needed. Vectors are sequence containers representing arrays that can change in size. In this article, we will learn how to insert elements into a vector using an iterator in C++. Example: Input: myVector = {10,20,30}; Output: Ve 2 min read How to Initialize 3D Vector in C++ STL? Initializing 3D vector refers to the process of assigning the initial values to the elements of 3D Vector. In this article, we will learn different methods to initialize the 3D vector in C++.The simplest ways to initialize the 3D vector is by passing the elements inside the initializer list. This me 4 min read Like