I feel like I'm just hoping that they can, but can a vector access bitset member functions?
For instance, can I do something like.
vector<bool> myvector;
myvector.count();
myvector.test(1);
If not is there anyway to make a bitset dynamically using the standard C++ libraries?
EDIT:
I want to use certain bitset functions (test, count) and want to use the constructor bitset (unsigned long val).
Basically I want to create a bitset for some arbitrary val then do some operations with test and count. Then I want to deallocate and recreate the bitset with a decremented val. I want to keep doing this until val is less than 8.
However, it doesn't seem like creating a dynamic bitset is possible and using vector which is dynamic, means I can use some of the nice bitset functions.
I have already answered to similar questions in S.O. I would suggest BITSCAN, a framework for dynamic bitsets specially oriented for fast bitscanning. It has been tested on Linux and Windows. A comparison with STL bitset and Boost's dynamic_bitset is available here.
Sounds like you don't actualy need a dynamic bitset. std::bitset is copy-assignable so you can do:
const size_t size = sizeof(unsigned long) * CHAR_BIT;
typedef std::bitset<size> my_bitset;
unsigned long val = 42;
my_bitset bs(val);
/* do work */
bs = my_bitset(--val); // assign with new, decremented val
Related
The objective is to convert a std::uint64_t (which is used as a bitmask), to a std::array<bool>.
This question is similar to the C# question How can I convert an int to an array of bool?, but for C++ and I'm looking for the algorithm with the bestest performance.
Given a std::uint64_t, which is used as a bitmask, I know it is possible to just loop over its content bitwise & bitcompare set the values to the ones at the same position in the std::array<bool>.
But in almighty C++ there has to be a more efficient way! Maybe some dirty casts, mallocs or whatnot? Everything is ok; I am on Windows / GCC, so even GCC-only features are totally allowed.
If you just need random access to the individual bits you can use a std::bitset instead of a std::array. That would look like
uint64_t mask = some_value;
std::bitset<64> random_access(mask);
// and now you can use random_access[some_index]
std::bitset does lack begin() and end() members though, so you can't use it with a ranged based for loop as-is. You can however iterate over a std::bitset using a regular index based for loop like
for (size_t i = 0; i < random_access.size(); ++i)
std::cout << random_access[i] << "\n";
I am new to stl and i came across a question where it is required to store a lot of pairs having same characteristics (like:- (string,int)) together. Can we store pairs in array and if so how? Thanks in advance..
You can try something like:
struct demo
{
string str;
int i;
};
std::array<demo, 5> arr; //for fixed size array of 5 elements
std::vector<demo> vec; //for dynamic size arrays
You want ...
to store a lot of pairs having same characteristics
The terminology is a bit wrong. You want to store many pairs of the same type (not "characteristics").
You could define your own struct as answered by Nipun, or use
std::array<std::pair<std::string,int>, 10> arr;
for an array of 10 pairs of strings and ints.
Spend more time on reading a documentation on standard containers and standard utilities. Perhaps you want some other existing thing (e.g. std::set with std::tuple, maybe with your own comparator).
Templates can (and often should) be combined since they are compositional. Be also aware of the rule of five.
There are times when indices need to be tightly packed (mesh geometry for example), where its useful to store indices as u32 instead of usize.
Is there a way to index a vector in Rust without having to explicitly cast to usize every time? eg:
vector[some_u32 as usize];
Casting in single instances isn't a problem, its just tedious when needed frequently.
Is there a way to avoid having to cast here?
No.
If it were your own type, you could implement Index<u32>, but it isn't and you can't.
If you're really, pathologically opposed to casting the index, you could write an adaptor type that does the cast, but that's getting a bit silly.
If you only want to index arrays of your own type, and you use a newtype for the index, then you can create an Index impl:
struct MyIndex(u32);
struct MyValue(u64);
impl std::ops::Index<MyIndex> for [MyValue] {
type Output = MyValue;
fn index(&self, idx: MyIndex) -> &MyValue {
&self[idx.0 as usize]
}
}
Now, you can index any array of MyValues with MyIndex. Instead of using u32 everywhere, you now have to use MyIndex. There's the newtype_derive crate to help you make the MyIndex behave mostly like a u32.
I am working on some core audio code and have a problem that could be solved by a variable array in a struct--a la Flexible Array Members. In doing a bit of looking around, I see that there is a lot of dialogue about the portability and viability of Flexible Member Arrays.
From what I understand, Objective-C is C99 compliant. For this reason, I think Flexible Array Members should be a fine solution. I also see that Flexible Array Members are not a good idea in C++.
What to do in Objective-C++? Technically, I won't use it in Objective-C++. I am writing callbacks that are C and C++ based... That seems like a point against.
Anyway, can I (should I) do it? If not, is there another technique with the same results?
You can always just declare a trailing array of size 1. In the worst case here, you waste a pretty small amount of memory, and it is very slightly more complicated to compute the right size for malloc.
don't bother. it's not compatible. it is messy and error prone. c++ had solutions which are managed more easily long before this feature existed. what are you tacking onto the end of your struct? normally, you'll just use something like a std::vector, std::array, or fixed size array.
UPDATE
I want to have a list of note start times (uint64_t) and iterate through them to see which, if any, is playing. i was going to add a count var to the struct to track how many items are in the flexible array.
ok, then a fixed size array should be fine if you have fixed polyphony. you will not need more than one such array in most iOS synths. of course, 'upcoming note' array sizes could vary based on the app synth? sampler? sequencer? live input?
template <size_t NumNotes_>
class t_note_start_times {
public:
static const size_t NumNotes = NumNotes_;
typedef uint64_t t_timestamp;
/*...*/
const t_timestamp& timestampAt(const size_t& idx) const {
assert(this->d_numFutureNotes <= NumNotes);
assert(idx < NumNotes);
assert(idx < this->d_numFutureNotes);
return this->d_startTimes[idx];
}
private:
t_timestamp d_presentTime;
size_t d_numFutureNotes; // presumably, this will be the number of active notes,
// and values will be compacted to [0...d_numFutureNotes)
t_timestamp d_startTimes[NumNotes];
};
// in use
const size_t Polyphony = 16;
t_note_start_times<Polyphony> startTimes;
startTimes.addNoteAtTime(noteTimestamp); // defined in the '...' ;)
startTimes.timestampAt(0);
if you need a dynamically sized array which could be very large, then use a vector. if you need only one instance of this and the max polyphony is (say) 64, then just use this.
When dealing with multidimensional arrays, is it possible to assign two different variable types to the array...
For example you have the array int example[i][j] is it possible for i and j to be two completely different variable types such as int and string?
Sounds like you're looking for:
std::vector<std::map<std::string, int> > myData1;
or perhaps:
std::map<int, std::map<std::string, int> > myData2;
The first would require you to resize the vector to an appropriate size before using the indexing operators:
myData1.resize(100);
myData1[25]["hello"] = 7;
...while the second would allow you to assign to any element directly (and sparsely):
myData2[25]["hello"] = 7;
No. That's not possible. You may want to look into using the STL map.
No, C++ only allows integer types (ex: int, long, unsigned int, size_t, char) as indexes.
If you want to index by a string, you could try std::map<std::string,mytype> but it gets complicated trying to extend that to two dimensions.
No, but you could use std::maps.
No, you can only use integer types as indices.
No you can't. You could achieve this with std::map though.