How can we compare a float to an int in general?
In my case I have a table T with column c(float).I want to extract all records where T.c > 20 .
Thx
I thought c> 20 would work, but it doesn't because float and integer are two different types
Related
First, you may wonder: Why do you need to have two dimensions, just create two one-dimensional arrays.
Well, here is my task: user types number of queries and then for each query he enters ID of the task, for ID = 0 I need to use first array ([][0]) and for ID = 1 second one, would be much prettier to do it without if statement (if (ID == 0)...blablabla).
Of course there is a way to do this with sorting algorithms (bubble, quick ...), but I'm kind of curios: Can it be done with std::sort(); function?
Thanks.
P.s. Here is quick example to clarify:
I so sorry to give such a bad example I,m gonna edit it, first here goes data that I have
2 1
14 5
5 7
3 45 `
so I need to sort It like this:
2 1
3 5
5 7
14 45
so when queries are entered for example id=1 number=2 just output array[2][1];
You can represent the data as a structure of some kind
struct data{
int id;
int other_data;
...
};
To sort by ID only...
{
std::vector<data> my_vec;
//Populate my_vec
std::sort(my_vec.begin(), my_vec.end(), [](const data& d1, const data& d2){
return d1.id < d2.id;
});
}
Three benefits:
If you later need other_data_2 to also stay ordered with id, it's trivial to add it
You can avoid using complicated multi-dimensional arrays
You can avoid using arrays (by using vectors)
I want to make structure storing value with two keys - 'x' and 'y'. I thought i will use std::map>. But I think it would be better to use just one std::map and combine both keys into one key (I will call it 'p').
So I need function p->(x,y) and function (x,y)->p. Both 'x' and 'y' are integer types with negative values allowed.
I think that it should be possible but I am surprised that I did not find tons of articles about it on the internet.
I have some ideas about how to do it, but all seems too complicated. Could you please give me some advices about how to do it or what topics to read about this problematics?
Example of possible mapping (but I need mathematical form of something like that): http://i.stack.imgur.com/UbVaM.png
Thank you for any advices :-)
I would just use a std::map<std::tuple<int,int>, int> for example, where your tuple is (x,y) and the value is z. Otherwise it will be difficult to keep a 1-to-1 mapping of p(x,y) to z.
How about using union?
union p_t {
p_t(int64_t c) : k(c) {}
p_t(int32_t a, int32_t b) : x(a), y(b) {}
int64_t k;
struct { int32_t x, y; };
};
std::map<decltype(p_t::k), int> m1;
std::unordered_map<decltype(p_t::k), int> m2;
p_t p = p_t(rand(), rand());
m1[p.k] = rand();
m2[p.k] = rand();
Live example: http://ideone.com/jebyG6
You could try and use functions similar to that used to show there are as many fractions as whole numbers This gives a unique integer for each fraction and a unique fraction for each positive integer. It works just as well for x, y coordinates. The two functions p->(x,y) and (x,y)->p are a little complicated but possible to write. You could adapt the idea using some sort of spiral shape.
.
Image from https://commons.wikimedia.org/wiki/File:Diagonal_argument.svg
I was wondering if it was possible to input two numbers into one cell of an array. Like in the case of having two points on a graph (1,-2) can I put them into the same cell and if so how? Thank you.
There are many ways to achieve this. Here are three common ones, all of which use a C array on the stack:
int graph[NUMEL][2];
struct { int x, y; } graph[NUMEL];
std::pair<int,int> graph[NUMEL];
I am trying to find a way to create a 3 dimensional vector with three different types, such that it is structured as:
Vector[long][int][double];
I have found plenty of examples that show how to create a 3d vector with a single data type, such as:
std::vector<vector<vector<int> > >;
But I can now find or figure out how to assign multiple data types to the vector.
You should use a struct if you wish to use all three types at the same time.
struct Vector3d{
long x;
int y;
double z;
};
//... or a union, if each entry only contains one type.
union NumberContainer
{
long x;
int y;
double z;
};
std::vector<Vector3d> vector1;//Vector of three types
std::vector<NumberContainer> vector2;//Vector that can contain one of three types per entry
vector1[0].x=1;
vector1[0].y=2;
vector1[0].z=3;
//vector1 contains... x=1, y=2,z= 3
vector2[0].x=1;
vector2[0].y=2;
vector2[0].z=3;
//vector2 contains x=undefined, y=undefined, z=3
Conceptually Vector[long][int][double] doesn't make any sense. You can have a vector of vectors of vectors of something. There's only 1 type of something in the end.
Take a step out of dimensionality. If you're just trying to contain 3 values per element in a vector you can do that a number of ways. Make a vector of a type that contains your 3 values: your own struct probably.
At the end of the day, your data structure has to hold something , and that something can only be of one type. Now, if you want to store multiple data types in each location of your vector, your "something" can itself be a struct of multiple different types.
It would help if you provided a little more context
I have a some data that needs to be stored and looked up efficiently. Preferably using C.
Each line of the data file is in the following format:
key1 key2 key3 data
where key1, key2, key3 are integers and data is an array of float.
I am thinking about converting key1,2,3 into a string, then use C++ std::map to map string to a float pointer:
std::map<string, float*>
Are there better ways of doing it?
Note: integer key1,2,3 has a range of 0-4000, but very sparsely populated. In another word if you go through all the values in key1, you will find < 100 unique int within the rang eof 0-4000.
You can use std::tuple to combine the three values into one:
std::map<std::tuple<int, int, int>, float *>
you do not have to use strings if your data limits for each key is from 0 to 4000
first generate the combined key as follows:
unsigned long ulCombinedKey = key1 + key2<<12 + key3 <<24;
after that you can use the map as you already stated in your questions.
A hierarchical map would do it:
map<int, map<int , map<int, list<float> > > > records;
and the access time would be good (logarithmic).
This way would be efficient if the range is very wide. Otherwise for 4000 the suggested shifts given in previous answer is faster and more efficient.
A hash provides very fast access to data, so you might want to use hashes to look up values from each of the three integers. This approach can be used in either c or c++.
For each line of data:
1. allocate space for the array of floats
2. store a pointer to the array of floats in an array of pointers
3. store the index of the pointer array in a hash based on on int1
4. store the index of the pointer array in a hash based on on int2
5. store the index of the pointer array in a hash based on on int3
This way, given int1, int2, or int3, one could look up a pointer array index, retrieve the pointer, then follow the pointer to the array of floats. This approach uses some memory, but not too much, given the problem said there are < 100 unique values for each of int1, int2, and int3.