STL - map containing a vector [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a map containing a string and a vector:
std::map<std::string, std::vector<int> > sorted_data;
The string is a persons name and the vector stores the ages of people with that name.
How do I access the vector to print out it's contents and perform calculations such as average on the data.
Any other explanation on the syntax and logic would be really useful as I am struggling with this.
Hope you can help.
Andrew

Your map contains many vectors associated with names, so to access one you could write the following, sorted_data["john"]. This will give you access to that particular vector.
For example to calculate the average you could iterate over all elements in the vector and sum them up.
int sum = 0;
for(int i = 0; i < sorted_data["john"].size(); i++)
{
sum+=sorted_data["john"][i];
}
float avg = sum/(float)sorted_data["john"].size();
You could also iterate over the map with iterators. Use the c++ reference site and try to understand the example code.

Related

How do I get the 10 lowest values in an array? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have an array of type double. How do I get the 10 lowest values?
double values[1000];
This is what I've come up before:
double similar[num_img];
copy(begin(values), end(values), begin(similar)); //copy values to another variable
int elements = sizeof(similar) / sizeof(similar[0]);
sort(similar, similar + elements);
So that I could get the 10 values. But what I'm actually after is the indices.. So sorting it would not help, I guess.
Sort the array and grab the first 10 elements (values[0] through values[9]).

find combinations of numbers stored in an array and store those combinations in another array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array of n size and I want to find combinations of k size. I don't just want to print those combinations but want to store those combinations in other arrays or any container. I read this creating all possible k combinations of n items in C++ but couldn't succeed in storing the combinations in other arrays. It is because I want to perform operations on those combinations. I'm seeking for any hints regarding this.
Thanks in advance.
Lacking a specific reason to choose something else, you probably want to store the results in a vector.
You can pre-compute the number of results quite easily -- N items taken K at a time will produce N!/K!(N-K)! total combinations.
At the risk of sounding condescending (which I don't intend) I'll point out that N! grows very quickly, so if the difference between N and K is very large, the result may easily be quite a bit larger than most computers can reasonably store.

C++ sort an array in descending order using a separate function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi there need help with an assignment in C++
I must randomly generate 100 numbers under 250 in an array and then sort them in descending order using a separate function.
#include <iostream>
#include<cstdlib>
using namespace std;
int main()
{
double sample[100];
int t;
for (t=0; t<250; ++t) {
sample[t] = (double) (rand()%100);
}
for (t=0; t<100; ++t) {
cout<< sample[t]<< "\n";
}
return 0;
}
I have managed to successfully complete part 1, would appreciate some help in the sorting of the random numbers.
http://www.cplusplus.com/reference/algorithm/sort/
It even has one of its examples directly relevant to your problem.
Sorting algorithms are one of the cornerstones of most beginner computer science courses and are a rather important study in the science itself. They are also very well documented on the internet.
You can ask for help with specific errors but asking for us to write it for you is not going to teach you anything.

how to create structure array with dynamic size [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to create a structure of array dynamically but i dont know the exact size so it will be added whenever i want...
Consider the following example..
Struct abc
{
double **ptrPoints;
int size;
};
i am defining pointer variable
abc* obj;
i dont know the exact size will be so i can not defile like
obj = new abc[size];
the elements will be added whenever condition satisfied.. i want it like vector but i dont want to use it ....
Please suggest me any way to write the functionality like this...
Thank u
Look up vector. Does all the leg work for you.
Wonder why you do not want to use vector. But you may initially declare array of sufficient size. Then maintain a counter of the elements being used up. If it has no space then use realloc.
Seems the only solution except vectors.
See THIS for realloc

What does the following code do? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was wondering what the following code does:
for (auto x:m) std::cout << x << " ";
I already know that auto is a way to leave it to the compiler to decide the type of the variable but I don't know what :m does.
It is a C++11 range-based for loop syntax described here: http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html
Here m should be a container, like std::vector. The code will iterate the container and put every element (accessed as x inside the loop) into the std::cout stream. Elements will be separated by space.
m is any type that follows the ranged concept (i.e. Container concept).
The loop iterates over all elements of m where x represents the currently iterated value.