HOW TO CREATE a multy MAP for 3 data sets - c++

I have a huge amount of data set. I wish to use array to store these data. In more deatil,
In my array, i want to use 3 columns such as Number number_of_points point_numbers. For this i can create a array like mypointstore[][] (for example mypointstore[20][3]). But my problem is that i want to store point numbers in column 3 like, 20, 1, 32, 9, 200, 12 and etc.(mypointstore[0][0]= 1, mypointstore[0][1]= 6 and mypointstore[0][2]={ 20, 1, 32, 9, 200, 12 }). I don’t know that is it posible to use array for this structure? If so, please help me to solve this problem.
I tried to use map like map<int,int,vector<int>> mypointstore; but i don’t know how to insert data into this map;
My some codes are here
map<int,int, vector<int>> mypointstore;
size=20;
For (int i=0; i<= size;i++){
Int det=0;
For (int j=0; j<= points.size();j++){//points is a one of array with my points
If (points.at(j)>Z1 && points.at(j) <=Z2){
//Here i want to store i , det and poiznts.at(j) like i in 1st colum, det in 2nd and
//pointnumber in 3rd colum) in each step of the loop it take a point
//number //which satisfied the if condition so it should be include into my
// vector of map
det++;
}
}
// at here i want to change total det value into 2nd element of my map so it like (0)(6)( 20, 1, 32, 9, 200, 12)
}
similar procedure for the next step so finaly it should be
(0)(6)( 20, 1, 32, 9, 200, 12)
(1)(10)( 20, 1, 32, 9, 200, 12, 233, 80, 12, 90)
(2)(3)( 3, 15, 32)

It sounds to me like you probably want a vector of structs, something like:
struct point_data {
int number;
std::vector<int> point_numbers;
};
std::vector<point_data> points;
I've only put in two "columns", because (at least as I understand it) your number_of_points is probably point_numbers.size().
If you're going to use the number to find the rest of the data, then your idea to use a map would make sense:
std::map<int, std:vector<int> > points;
You could use a multimap<int, int> instead of map<int, vector<int> > but I usually find the latter more understandable.

Related

Logical Size (number of elements) in array

I have an array (defined below) and I want to find the number of elements in it and send in mehtod call.
So, I have this:
const int MAX_SIZE = 20; // Maximum size of data array
double givenDataPoints[MAX_SIZE] = {0, 2, 3.8, 5, 9, 16, 16.2, 17, 18, 19, 19.5};
And i want to get
int logicalSize = //this should be 10 because I only put in 10 numbers, not 20
How do I do this?
I highly recommend using the standard library for that:
http://www.cplusplus.com/reference/array/array/size/
But you can also do:
sizeof( givenDataPoints ) / sizeof( givenDataPoints[ 0 ] );
But you are going to get 20 because you told the compiler to allocate space for 20 elements.
You need to keep track of what is considered to be an empty element.
Here is a similar question: Find the count of elements in array in C++
I hope it helps.

Sort Array but keep row indexes

I have a array like this:
names, state, houseprice, carsprice
john, WI, 200, 100
jimmy, MI, 100, 90
...
How do I sort the houseprice and car price without changing the elements in that same row? Like sort the whole row of "john" based on their houseprice and carprice against another row in the array but keep everything in the row together.
this is what I have so far,
for (int i=0;getline(file,(names[i]),',');i++)
{
getline(file, states[i], ',');
getline(file, houseprice[i], ',') ;
getline(file, carprice[i]);
}
how I want to sort houseprice and carprice against another row without losing the name and states along with that row.
Just project the field you are interested in as part of your comparison function object but otherwise treat the objects as one unit:
house array[] = { ... };
// sort by houseprice; similar for other attributes:
std::sort(std::begin(array), std::end(array), [](house const& h0, house const& h1) {
return h0.houseprice < h1.houseprice;
});
you need to implement a vector that maps "original" order to the "sorted" order,
so the sort algorithm result will be a mapping vector and the function that draws the output table need to draw it by the order of the mapping vector.
for example, if after sorting all lines stay on first order position and only lines 4,5 swap
places the mapping vector will be:
Index: 0, 1, 2, 3, 4, 5, 6, 7, ...
Data : 0, 1, 2, 3, 5, 4, 6, 7, ...
since your question is quite general - so does the answer
You can use a vector of struct
eg:
struct PersonalInfo
{
string name;
string state;
int housePriceCents;
int carPriceCents;
}
std::vector<PersonalInfo> infos;
Then use sort with a compare function passed in, or define the < operator in your struct, so that structs with lower prices are considered less than structs with higher prices.

Sorting a 2d array

I am trying to sort two sets of data, that are either in a 2d array or parallel arrays, either way it makes no difference but I cant seem to figure it out. Here are the two arrays:
/////way one///
int id[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int numDetected[10] = {40, 21, 2, 19, 45, 32,43, 90, 100, 8};
or
int 2dArray[2][10];
it makes no difference, but I cant seem to figure this out.
I want to order the arrays into a new array, (whether it is a 2d array or parrellel arrays) by the numDetected amount. So the largest numDetected is at element zero and the smallest at the end. But while doing that, I want to keep the id associated with that in the same element as the numDetected. So if numDetected[2] is the largest, I want numDetected[2] and id[2] to be the first elements in the new arrays.
Can anyone help me out?
struct values
{
int id;
int detected;
} data[10] = ...;
// intentionally reversed to cause sort in descending order
bool operator<(const values& left, const values& right) { return left.detected > right.deteted; }
values *begin = data, *end = (&data)[1];
std::sort(begin, end);

c++ arrays, how to add a new row of values in the same array?

how do you create a new row of values in an array from user input or cin?
say theres a row of values already in the array and you need to add a second row of values
but not added to the first row, and how would you put the braces and the comma in, does the user put it in or is there something that will automatically put the bracers and comma in
int test [] = { 1, 21, 771, 410, 120711 },
{ 1, 2, 3, 4, 5 };
Without very bad and dirty tricks this is not possible. Better use list or vector (which is the nearest to an array). The other possibility is to use pointers and to extend it create a temporary memory, copy the old data and then add the new.
There is no way to change the size of array while still preserving its contents. The only way to change the size of an array at all is to use the new operator to allocate dynamic memory to a pointer, but this will destroy any data the array previously held. If you want to have a re-sizable array, you should probably use std::vector.
If you're keen on using c++11 you can keep your initialiser lists with std::vector like so:
#include <vector>
int main()
{
// initialise
std::vector<std::vector<int>> test = { { 1, 21, 771, 410, 120711 },
{ 1, 2, 3, 4, 5 } };
// add new data from user
test.push_back({9, 8, 7, 6, 5, 4, 3, 2, 1});
}
You're asking for a two-dimensional array. This is declared like this:
int test[][5] = {
{1, 21, 771, 410, 120711},
{1, 2, 3, 4, 5 },
// Add more if you want.
};
The first array is accessed through test[0], the second through test[1], etc. The first element of the first array is test[0][0], the second test[0][1] and so forth.
Note that this is an array with a static size. You can't change it at runtime. If you know in advance how many rows you need, you just declare it as:
int test[NUMBER OF ROWS][NUMBER OF COLUMNS];
and then fill it with values later. But you cannot change the size. If you want a fully dynamic array, then you should use a vector of vectors:
std::vector< std::vector<int> > test;
You then add rows with:
test.push_back(std::vector<int>());
and add elements to each row with:
// Adds a number to the first row.
test[0].push_back(some_int);
Access happens the same way as with the static array (test[0], test[0][0], etc.)

How do i select the other column in a 2-dimensional array?

How do i select the second column of a 2-dimensional array.
I have this array with about 30 values (LKT) and from there I have a 2-dimensional array (ScaledValues). The 2nd column of this 2-dimensional array will be filled with a scaled version of the original LKT array.
Initially, ActiveArray variable points to the LKT array. However, when I populate the 2nd column of the array in ScaledValues with scaled values of the first LKT array, how do I move ActiveArray to now point to the second column as the active array that i'll be using? i.e. After i fill the second column with the desired scale values, I'd like to work with those values and I want to use ActiveArray variable to denote that this new column is the active array.
I know there are other ways to do this i.e. I could create 2 separate individual arrays but I have to use the format that you see below. please assist. thanks.
Please let me know if i need to make my question clearer.
Thank you very much.
static const unsigned int LKT[30] = {
30, 29, 28, 27, 26, 25, 24, 23, 22,
21, 20, 19, 18, 17, 16, 15, 14, 13, 12,
11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
static unsigned int ScaledValues[30][2];
static volatile unsigned char ActiveArray = 0;
Reverse the ScaledValues array declaration:
unsigned int ScaledValues[2][30];
for (int i = 0; i < 30; i++) {
ScaledValues[0][i] = LKT[i];
ScaledValues[1][i] = scale(LKT[i]);
}
// Also need to make this a pointer
unsigned int * ActiveArray = ScaledValues[0]; // Original values
ActiveArray = ScaledValues[1]; // Scaled values
the easier way to manipulate this array could be done as following
first the definition should be in this way
static unsigned int ScaledValues[2][30];
then to copy LKT to the first line (and not column)
memcpy(ScaledValues[0], LKT, 30*sizeof(unsigned int));
to access to the second line of the ScaledValues array is
ScaledValues[1]