Cast vector to iterator? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
I have the following code
vector::vector(const vector& arg)
:sz{arg.sz}, elem{new double[arg.sz]}
{
copy(arg, arg+sz, elem);
}
How is arg converted into an iterator here? whats the mechanism called that defines the conversion internally of vector outputting the needed iterator?
Thanks

Related

how to insert a char* into the end of a vector<char>? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
vector<char> vec;
vec.push_back(0x1);
char* a = "qwe";
I want to push a to the end of the vector.
You can use the .insert() member function with pointers standing in as iterators.
vec.insert(vec.end(), a, a+strlen(a));

Can you have a string and a integer in one vector? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I just have a general question. Can you have a string and an integer in one vector? I am planning to display card names and numbers. I want to display both and wanting the numbers to be added up at the end. Can I do this?
You can have std::string and int in one std::vectorusing std::variant
using ElementType = std::variant<std::string, int>;
std::vector<ElementType> v;
v.push_back(std::string("I am string"));
v.push_back(1);

C++ references and modification [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Lets say I have
void swap(int &x, int &y){
//do swap here
}
Why is it legal to do: x = y inside of the function so that it assigns the VALUE of y to x?
Why isn't some sort of dereferencing needed?
There's no dereferencing needed since references are not pointers. They are aliases to the referenced object.

Return a pointer to pointers in a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a matrix in one class. That matrix is allocated dinamically, I want to encapsulate it.
Here is my matrix declaration in the Header file:
float** matrix;
And here is the declaration of get method:
float *getMatrix();
Is everything correct so far?
I don't know how to work with pointers in that case. How would the get function look like?
if you want just to return matrix you should use
float** getMatrix();

How to fix this C++ global-pointer-to-object hack? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Currently, I have code like:
static YAML::Node *doc;
...
__attribute__((constructor)) void inityaml() {
doc = new YAML::Node;
parser.GetNextDocument(*doc);
}
The question is, is there any more C++-conventions-ish way to perform this task, like the use of a global reference or something?
Why not avoid heap allocation altogether?
i.e.
static YAML::Node doc;
...
void inityaml() {
parser.GetNextDocument(&doc);
}