Array, class and function - c++11

I don't know what to do with this program.
I have a class like this:
const class infoCity{
public:
queue <int> nei;
int customUni;
}
and in main I have an array of cities like this:
int number;
cin>>number;
InfoCity cities[number];
after that I need to create a function which will tell me closest way from one point to another so I started write and the problem show up because I don't know how to get cities[number].
const int searchWay(int start, int finish){
??--- how do i get information from main cities[number] ?? }

From what I understand your only issue is that you don't know how to get the field from the array. I'm not sure if you are filling the array correctly or if you have access to the array from the function you are creating, but to get the information you just append a period with the field name afterwards:
cities[number].customUni
Here is a link on how to use arrays:
http://www.cplusplus.com/doc/tutorial/arrays/

Related

SystemVerilog - How to get the number of enumerated types at compile time

I trying to find a way to get the number of possible enumerations in an enum type at compile time. I need this for initializing a templated class that uses enumerated types.
I am curious if there is a utility function (or system task) that gives this. It would be similar to $size() but for enumerated types. However, I can't seem to find a function for that. After doing a lot of research, it doesn't seem to be possible.
Here is an example I am trying to do:
typedef enum {RANDOM, STICKY, SWEEP} bias_t;
// can be parameterized to pick another enum type at random
class enum_picker #(type T = bias_t); //type must be an enumerated type
local T current_type;
local const int weights[$size(T)]; //<--- How do I get the number of enumerated types?
function T pick_type();
... some code ...
endfunction
endclass
So for the variable weights, it is an array of weights in which its size is the number of enumerated types. Right now it is 32 because of the $size() call but that is wrong; in this particular code example, the array size should be 3.
Is there a way to do this? Or is this simply not allowed in SystemVerilog?
You probably don't want to set up weights as a const; you would not be able to set values into it. You can use the num() method to get the number of enumerations.
class enum_picker #(type T = bias_t); //type must be an enumerated type
local T current_type;
local int weights[];
function new;
weights = new[current_type.num()];
foreach (weights[i]) weights[i] = $urandom_range(10);
endfunction
function T pick_type();
endfunction
endclass

Iterating over const_iterator

ALL,
I have a function with the following signature:
void foo(const std::vector<Bar *> &myvec);
Inside this function I need to loop thru the members of the vector and perform some operations.
So, I tried this:
for( std::vector<Bar *>::const_iterator it = myvec.begin(); it < myvec.end(); ++it )
{
// modify properties of Bar * pointer
(*it)->SetSomeValue( baz );
}
however this code asserts since the iterator is constant.
Now obviously the vector is constant, which means that the function shouldn't be modifying myvec.
What's the best solution here?
Can I use const_cast here to remove constness? It would be kind of hack-ish, but if it works.
But I feel there must be a better solution.
TIA!!
You should use the myvec.cbegin() method instead of myvec.begin(), to ensure that you are not modifying the object the iterator points to.
Of course, for myvec.end(), use myvec.cend() accordingly.
The iterator itself doesn't need to be a const_iterator, in the contrary, you want to modify the objects it gives you - set_...() sounds like a non-const activity.

C++ std::sort of const struct

I have the following struct in a vector
struct ub_node {
const size_t index;
const double ub_dist;
bool operator<(const ub_node &rhs) const { return ub_dist< rhs.ub_dist; }
};
I would like to sort that vector. I have tried using std::sort but I get a compile error:
error: use of deleted function ‘ub_node& ub_node::operator=(ub_node&&)’ with a reference to the line where I do std::sort(result.begin(), result.end());, where result is of type vector<ub_node>.
As far as I understand, the const does NOT affect the execution time, but merely ensures that the programmer (me) does not do anything stupid. If this is the case, I might remove the const and try to ensure that I do not change the node afterwards. Can someone confirm this? Or help me to sort it?
As far as I understand, the const does NOT affect the execution time, but merely ensures that the programmer (me) does not do anything stupid.
Correct.
Sorting a vector swaps the elements around, which is not possible if the elements have immutable data (with your ub_node type you can't even remove or insert an element anywhere except the end of the vector, because that also needs to modify existing elements).
I suggest you remove the const from your members, so the type is possible to modify, and then structure your code so that parts of the program which shouldn't modify it only interacts with the class through const ub_node& or const ub_node*.
That means that the class is modifiable when necessary, but won't be modified by parts of the program that aren't supposed to modify it.

error CS1729: The type `UnityEngine.Random' does not contain a constructor that takes `1' arguments

I've done some elementary coding in the past, and I'm now learning Unity and trying some things with C#.
My problem:
I have a list of objects that have their own id number in the range of 1-50. I want my game to pick one object at random instead of going over the list in order. The first step would be to pick the initial id to be some random number, but I only get the error: "error CS1729: The type UnityEngine.Random' does not contain a constructor that takes1' arguments". I understand that I should give more argument for the constructor, but I need help in seeing how, since the code looks fine (if simple) to me.
Anyway, it goes like this at the moment:
public int id;
public int randomid;
public void RandId(int id)
{
Random randomid = new Random(Random.Range(1, 51));
id = randomid;
return id;
}
Here id is the identification number of the objects, randomid is for randomizing it, and I use Random.Range to create the wanted range (1-50). It seems I need to give more arguments to Random.Range, but it already has both min and max.
Can you give me some advice?
There are some other things wrong w/ your code, but this should be what you need.
public void SetIDToRandom(out int id)
{
id = (int)Random.Range(1, 51);
}
Addendum:
Random is a static class, you don't directly instantiate it.

Passing a boost::bimap between functions

I'm new to the bimap functionality of the Boost libraries, and I'm having trouble passing a bimap into another function. My bimap looks like this:
typedef boost::bimap< int, int > bimap_type;
bimap_type bm;
I have an add_values() function that adds a set of values to the bimap:
add_values(int a, int b)
{
bm.insert(bimap_type::value_type(a, b));
}
I then have a function that is meant to set the values of the bimap by getting them from a Singleton Class:
void set_values()
{
MyClass::instance()->get_values(bm);
}
And, in MyClass, get_values() looks like this:
void get_values(bimap_type myBimap)
{
myBimap.add_values(3, 5);
}
However, MyClass does not recognise 'bimap_type'. I try putting the typedef in a separate header file and including that in MyClass, but I get the error message:
'class bimap_type' has no member named 'add_values'
How can I successfully pass the bimap to this Singleton Class in order to fill it with values from the Class? Does anyone know?
Thanks a lot.
Er, boost::bimap itself doesn't have an add_values method and it's hard to tell from these code fragments why you're suddenly expecting one to appear.
Consider renaming your functions: set_values() that calls get_values() that calls add_values() is one confusing call chain...
When you need to modify an object in a function, you have to take it by reference (or a pointer). The idea is that you must work with the same object inside and outside of the function. If you pass by value, function will see a copy, so anything it does with it does not reflect on original object.
// formerly known as add_values()
void initialize(bimap_type& bm, int a, int b)
{
bm.insert(bimap_type::value_type(a, b));
}
And this is how you will call it:
initialize(myBitmap, 3, 5);
Make sure to update your whole call chain to pass by reference where appropriate, because currently your get_values() works with a copy too.

Resources