3,090 questions
Advice
1
vote
5
replies
162
views
C++ convert vector element to string
Why can't you just do this?
int index = 0;
std::string example;
std::cin >> example;
std::vector<std::string> list;
list.push_back(example[index]);
I'm a complete beginner in c++ and I ...
Advice
1
vote
13
replies
92
views
How to create a map-like interface for std::vector, or how to treat a vector as an index-value map?
I do things with maps (could be std::map or another map from the standard library, a boost map, a custom map from another library, etc):
template<class T>
void do_map_stuff(T const& t)
{
...
Best practices
0
votes
12
replies
6k
views
Avoiding templates when working with different std::vectors
I have the following problem.
There is a class A and B where B derives from A.
There is a third class C which needs to hold a std::vector<A> or std::vector<B>
I want the data to be as a ...
0
votes
0
answers
181
views
Can a segfault occur if you are doing iterator arithmetic ops beyond limits?
it -> a random valid vector iterator
const int kLookAheadIndex -> a random number
auto it_ahead = std::max(it - kLookAheadIndex, path.cbegin()); // get a look-ahead point to look at
I know ...
0
votes
1
answer
105
views
Using tbb::parallel_for with reserve and emplace does not copy all elements between std::vectors
I am trying to populate a std::vector container using another std::vector and tbb::parallel_for method as shown in the following code snippet:
#include <tbb/parallel_for.h>
#include <eigen3/...
2
votes
2
answers
212
views
How to return a list of selected elements of some underlying container in a typesafe manner?
Im wondering if there is a better, more typesafe, way to return "a selection" than I do at the moment.
If we only ever have one selected element at a time, we can just return a reference, ...
3
votes
1
answer
257
views
Copying and Padding a Vectors rows quickly
System is:
Ubuntu Linux 20.04.6 LTS
g++ version 9.4.0
IDE QTCreator
I have two vectors (set and subset), subset needs to be transferred into set, at location (0,0), such that, the rows are padded ...
4
votes
2
answers
207
views
"no matching function for call" in initialiser list for char [] [duplicate]
I have the below code. The first push_back fails, the second, with a constant string, works.
#include <vector>
#define MAX_LENGTH 10
struct bar
{
int barInt;
char barChar [...
1
vote
0
answers
58
views
What is the category of transform view based on vector? [duplicate]
What is the category of std::views::transform based on a std::vector? What is the category of the view's iterator in the following code?
#include <iterator>
#include <memory>
#include <...
0
votes
1
answer
203
views
Removing an element from a map of vectors
std::map<unsigned long, std::vector<Foo*> > fKeys = myObject->GetFoo();
for (auto it = fKeys.begin(); it != fKeys.end() && !found; ++it)
for (auto it1 = 0; it1 < (*it)....
-2
votes
2
answers
268
views
Is there a better way to keep ids in a vector sequential in C++?
I have a simple C++ program that manages as a todo list. The Todo, class contains a vector of Item to track each todo action and their id. The key constraint is that the ids must be kept strictly ...
4
votes
2
answers
228
views
Can the back() iterator of a vector be safely assumed to be the end() iterator after a pop_back()?
My problem is the following :
std::vector<struct pollfd> vec = { ... }; // Actually a member variable on a Server object
for (auto iter = vec.begin(); iter != vec.end(); ) {
if (...
-3
votes
1
answer
249
views
Why can't the compiler deduce the element type of `std::vector`?
I want to use auto more in my code and came up with the following example:
#include <vector>
int main() {
auto v{std::vector{}};
for (auto i{0}; i < 10; i++) {
v.push_back(i)...
3
votes
2
answers
125
views
How to instantiate std::vector using decltype?
This is my test code:
#include<vector>
#include<iostream>
#include<type_traits>
using std::vector;
using std::is_same_v;
using std::cout;
using std::endl;
int func()
{
struct ...
7
votes
1
answer
255
views
Directly initialize std::array with a fixed count of elements from std::vector [duplicate]
The elements in the array are not default constructible therefore we need to initialize the array directly from elements in std::vector.
#include <array>
#include <vector>
class foo {
...