What does the following code do? [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 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.

Related

Modifying elements of a string vector in c++ [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
How can I append a string to an element of an string vector?
I consistently get segmentation fault.
Appending str to the ith element of vec:
vec[i] += str;
Assuming vec is something like a std::vector<std::string> and the ith element exists.

STL - map containing a vector [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 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.

return this-> command in C++ [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
return this->
what is that mean in C++
..
using namespace std;
IOM ConfigurationManager::getIOM(int iomId) {
return this->IOMs[iomId];
..
the relevant part of the whole code is above.
The code that I wrote is from a huge project which was waiting for someone to finish. I am not good at C++ but I need to learn more not to lose that job. Anyway, the project is full of "return this->...." which I thought unnecessary, that's why I asked is there smt special that we should use that notation
This piece of code simply means that the IOM at index iomId in the IOMs array in the ConfigurationManager object is returned. Note that the this->IOMs is the same as IOMs in this case, thus it seems the this is only there for clarity.
this is a pointer to the current object. The -> operator allow you to access a member inside a pointer to an object.
Thus return this->IOMs[iomID] returns the IOM object in the current ConfigurationManager at index iomID.

Finding value of int x [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
int x;
x=x+30;
cout << x;
the output will be 33, why is it so?
I didn't even declare x as 3.
Can someone guides me? Thanks!
Using an uninitialized variable is undefined behavior. You got 33 due to an unreliable sequence of implementation quirks. The program is free to produce any value at all, fail to compile, or hire an assassin to stab you.
In C++, variables are given space (memory allocation) by default equal to the size of the variable, but they are not given values by default.

how to overwrite portion of one string with another starting from a given position? [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 8 years ago.
Improve this question
I mean the way that is done in editors with insert key on.
So having string like:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The effect will be:
~~~~~~~~~~Hello!~~~~~~~~~~~~~~~~~~
that is without changing length of the string.
Overwriting a portion of a string is done with one of the several overloads of std::string's replace member function, for example:
string str = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
string rep = "Hello!";
cout << str.replace(5, rep.size(), rep) << endl;
You can play with this example at ideone [link].
The simplest solution is probably to use std::copy, with the
appropriate iterators:
std::copy( newText.begin(), newText.end(), str.begin() + n );
Just be sure that the target string is big enough.