How to convert std::vector to a plain array (C-style array) in C++?
Hello everyone! I'm writing a C++ program and ran into a problem. I have a std::vector that I'm filling with data, but one of the libraries I'm using expects a plain C-array (int*) as input. How can I get a raw pointer to the vector's internal data to pass it to the function?
Here's an example of my code:
\n\n#include <iostream>\n#include <vector>\n\nvoid process_array(int* arr, size_t size) {\n for (size_t i = 0; i < size; ++i) {\n std::cout << arr[i] << " ";\n }\n}\n\nint main() {\n std::vector<int> vec = {1, 2, 3, 4, 5};\n \n // How to get int* from vec?\n // process_array(vec, vec.size()); // Error!\n \n return 0;\n}\n\nI've heard there's a method <
No answers yet.
Log in to leave an answer
Blogs
Best IDEs and Tools for Go Development: Advanced Level
Testing in JavaScript: unit, integration, e2e from A to Z
Concurrency in Swift: Threads, Goroutines, and async/await — How Not to Go Crazy
WebSockets in TypeScript: Building Real-Time Applications from Scratch