Is it possible to modify single value in std::discrete_distribution? I can't find a simple way of doing this. I was thinking of initializing it with std::vector with assigned probabilities and modifying it everytime I want, but reinitializing discrete_distribution everytime seems to be not the best idea.
You can't, there's no following function in std::discrete_distribution.
You can get probabilities, but not set, so, there is only one way - reinit discrete_distribuion (probably you can use vector of predefined destributions).
Related
If I have array int a[n], and I have a function which has this array as its argument. This function takes the array, does some changes in the array, and then returns a boolean value. All I want to do is undo the changes done by the function if the returned value is false (return the array as it was). How can I achieve this?
You can do this however you want. There is no "one right way". It depends on the specifics of your situation. Here are a few possibilities:
Keep a copy of the array and revert to the copy if you need to "undo" the function.
Write a version of the function that goes in reverse and call that version if you want need to "undo" the function.
Have the function keep track of the previous values of any array entry it modifies so you can apply that tracking to revert the function.
Don't have the function actually modify the array but instead have it create a set of modifications. Only modify the array if you decide that's what you want to do.
I highly recommend option 4. You can have a "modified array" class that provides precisely the same API as the array that also includes a reference to the real array. It can have an apply member to change the real array. That way, you can just throw away the instance of the "modified array" class if you don't want to keep the changes.
Since the modified array class provides the array API, it can also modify another instance of the modified array class. This provides as many layers of nesting as you may need.
You have to backup the input array into a local array inside of the function (Before doing changes to input array). There after write the function logic. Finally check the value which is going to return by the function. If it is false then copy the backup array data in to input array.
I am creating a genetic algorithm that classifies set of data for which I need to generate random sequences of 1s,0s and 2s for defining a rule
2 represents it being in 2 states a 1 and 0. I am trying to use Map STL for mapping the random set of rules generated and the output for each rule. I need the Map key to be dynamic/ changing every iteration/generation to be filled by new population of rules.
I realise I have option of using pointers which complicates my code and will have readability issue.
The other option I know about is copying the key elements and value and deleting it so it can be replace by the new rule.
So, My question is:
1). Is it better to use vector and my own mapping algorithm? The only problem being is I want to be efficient and fast as I will be dealing with 2000 or more data.
2). Are there any other STL which I can use no libraries that I need to download pls.?
3). Shall I just use Map and keep reseting the elements in the map each time so I can initialise them again?
which method would be effective?
Open to any other suggestion or advice.
If you don't want data to be in sorted order you could consider std::unordered_map<>.
Have a look at this for some benchmarks.
The S of the famous Object Oriented Programming design stands for:
Single responsibility principle, the notion that an object should have
only a single responsibility.
I was wondering, can this principle, be extended even to arrays, variables, and all the elements of a program?
For example, let's say we have:
int A[100];
And we use it to store the result of a function, but somehow we use the same A[100] to check, for example, what indexes of A have we already checked and elaborated.
Could this be considered wrong? Shouldn't we create another element to store, for example, the indexes that we have already checked? Isn't this an hint of future messy code?
PS: I'm sorry if my question is not comprehensible but English is not my primary language. If you have any problem understanding the point of it please let me know in a comment below.
If same A instance is used in different program code portions you must follow this principle. If A is a auxiliary variable, local one for example, I think you don't need to be care about it.
If you are tracking the use of bits of the array that have been updated, then you probably shouldn't be using an array, but a map instead.
In any case, if you need that sort of extra control over the array, then basically, you should be considering a class that contains both the contents of the array and the various information about what has and hasn't been done. So your array becomes local to the class object, as do your controls, and voila. You have single responsibility again.
When calling boost::get on a graph, the return type is boost::associative_property_map<Graph, PropertyType>. I'd like to get a copy of this map, and I'd like to store it in a boost::vector_property_map. However, they are not compatible enough to allow simply assigning to the vector map. Looking at the source of property_map, it seems boost::associative_property_map does not have any access methods at all (the vector variety at least exposes "storage iterators").
Is there a way? Or do I have to find the keys somehow and loop over them?
I am sorry, but simply using boost::associative_property_map, you won't be able to retrieve iterators over contained items. You must use an alternate way to iterate over them -- iterate over edges or vertices, i guess, and put them into a boost::vector_property_map.
But if you have to do that, it means you are deconstructing the whole idea of having the common interface that property maps provide. Can't you templatize the algorithm which currently needs vector_property_map to have it accept other property_map types ?
i want to see the number of appearance of words from some phrases.
My problem is that i can't use map to do this:
map[word] = appearnce++;
Instead i have a class that uses binary tree and behaves like a map, but i only have the method:
void insert(string, int);
Is there a way to counts the words apperances using this function?(because i can't find a way to increment the int for every different word) Or do I have to overload operator [] for the class? What should i do ?
Presumably you also have a way to retrieve data from your map-like structure (storing data does little good unless you can also retrieve it). The obvious method would be to retrieve the current value, increment it, and store the result (or store 1 if retrieving showed the value wasn't present previously).
I guess this is homework and you're learning about binary trees. In that case I would implement operator[] to return a reference to the existing value (and if no value exists, default construct a value, insert it, and return that. Obviously operator[] will be implemented quite similarly to your insert method.
can you edit "insert" function?
if you can, you can add static variable that count the appearnces inside the function