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 have a std::vector<int64_t> myVec in which I want to store a string (text). My understanding is that i cannot reserve space and write to myVec.data(), as this would be undefined behavior. What would be the non-hackish way to do this? I'm assuming the last int64_t will have to be filled with padding zeroes.
I'm using C++14.
If you are constrained to vector the only solution is resize() to required size and strcpy/memcpy into data()
Why would you store text in int?
Is your intention to store digits made from string?
If so, you shall:
std::vector<int64_t> myVec;
myVec.push_back(atoll("100"))
Related
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);
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 4 years ago.
Improve this question
Is there any way I can append the size of a particular variable? For instance, if I have:
static_cast<char>(0x0147)
an 8 bit char and I want to increase it to say 16 bits without changing the data type, is that possible?
No.
The size of the object is not only related to its type; the size is defined by the type.
You cannot change one and not the other.
Just initialize a new int16_t from this char if that's what you want.
Or, you could have a vector<char> and add new elements to this collection as needed.
(Shifting has nothing to do with it; that's about transforming data.)
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
How do you store contents in a vector and then sort them without creating a class?
Doing a class project and it requires to not use a class. The books are not helping.
This page has a good example, assuming you can put all of the elements in the vector at once. If you need to put them in one at a time, use std::vector::push_back.
Something like std::vector<std::string> v; v.push_back("hello"); std::sort(v.begin(), v.end());. Get better books.
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
I found implementation using 2D array http://www.geeksforgeeks.org/dynamic-programming-set-10-0-1-knapsack-problem/. But how to implement it using 1D array and if it is not possible then why?
Note that each of values in the current row uses only elements of the current and previous rows. Hence you can implement the algorithm with an array K[2][W], which is the same as using an array K[2*W] with some additional trivial index calculations
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
Is there some function in boost library which can sort key-value pairs?
e.g. keys being an array of double variables, and values are some index (integer) of the array.
What you probably want is a std::map<My_Double_Array, size_t> along with your own My_Double_Array class that wraps your array of double variables and provides a operator <() for sorting. Or simply std::map<std::vector<double>, size_t> might be all you need.