pointer to std::map fails to insert [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am very confused. Why does this work:
double doubleValue = 20;
NcVar variable = {some process obtaining an instance}
map<NcVar,double> th;
th.insert(std::make_pair(variable, doubleValue));
and this fails:
double doubleValue = 20;
NcVar variable = {some process obtaining an instance}
map<NcVar,double> *th = new map<NcVar,double>();
th->insert(std::make_pair(variable, doubleValue));
That means, the first variant ends up with one key/value-pair, while the second leaves the map unchanged (0 entries)?

Works for me:
#include <map>
#include <iostream>
using namespace std;
int main(){
typedef map<int,float> mapp;
mapp map1;
map1.insert(make_pair(1,1.1));
mapp * mp2 = new mapp();
mp2->insert(make_pair(2,2.2));
cout << map1.begin()->second << endl;
cout << mp2->begin()->second <<endl;
return 0;
}
And output:
$g++ map_test.cpp
$ ./a.out
1.1
2.2

Thanks for the help, guys. I feel kinda stupid now. The assumption that the map was empty was based on the appearance in the debugger. I am using XCode as IDE and when using a pointer to map, it would simply mess up and display the map as empty. Using cout revealed the truth.

Related

How to add decimal to floating point number with non-decimal value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have following program and i want the output to be 10.0
what line of code I have to add in function fun so that i have desired output.
#include<bits/stdc++.h>
using namespace std;
float fun(float a)
{
return a;
}
int main()
{
float a = 10;
cout << fun(a);
return 0;
}
I tried using setprecision() but it is often used with cout. How it can be used when returning the output? I am stuck here.
Nothing to stop this
float fun(float a)
{
cout << fixed << setprecision(1);
return a;
}
But that's stupid code for a stupid puzzle. What does this have to do with real programming?

Having a problem calculating the compounded Interest [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I tried as I/P: 100, 10, 1 and it works fine so CI = 10 and total = 110
But with other example I can't get right answer can you please tell me what to do.
I tried to look at other questions here but I can't understand what the wrong I did.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float amount,rate,time_,total,compoundedInterest;
cout<<"Amount: ";cin>>amount;
cout<<"Rate of Interest: ";cin>>rate;
cout<<"Time: ";cin>>time_;
compoundedInterest=amount*pow((rate/100),time_);
total=amount+compoundedInterest;
cout<<"Compounded Interest: "<<compoundedInterest;
cout << endl;
cout<<"Total: "<<total;
return 0;
}
Please Help me figure what is the problem.
Code:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float amount,rate,time_,total,compoundedInterest;
cout<<"Amount: ";cin>>amount;
cout<<"Rate of Interest: ";cin>>rate;
cout<<"Time: ";cin>>time_;
compoundedInterest=amount*pow((1+rate/100),time_)-amount;
total=amount+compoundedInterest;
cout<<"Compounded Interest: "<<compoundedInterest;
cout << endl;
cout<<"Total: "<<total;
return 0;
}
Problem:
The main problem with your CI rule it is wrong that is why you will find wrong O/P.
You can use this code which will works fine with you.
Rules:
Note:
I used the first rule to calculate CI here.
Resource:
Link1
Link2

Returning position of int [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
The task is to return the leftmost position of given int A in larger int B (counting from 0)
A = 53, B = 1953786,
return: 2
I've found similar problem, but in that solution java function "indexOf" was used, is there a possibility to do it in other way?
The easiest way to do this would be to convert them both to std::string, and then you can use functions of std::string to find the index, similar to how you'd have indexOf for Java.
#include <iostream>
#include <string>
int main()
{
int a = 53;
int b = 513953786;
std::string number = std::to_string(b);
std::string numberToFind = std::to_string(a);
auto index = number.find(numberToFind);
std::cout << index;
}
This will return 4, since I moved the 53 to be at the 4th index to show it won't find the earlier 5 or 3 I added, per the advice of #Blastfurnace in the comments
What about using the c++ function: std::find()? You could then do something like this:
int getIndex(int a, int b){
std::string a_str = std::to_string(a);
std::string b_str = std::to_string(b);
return a_str.find(b_str);
}

C++ how to print union name integer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to print integer in C++:
int n5x = 1;
int n5y = 2;
[...]
int value = 5;
cerr << n+value+x << n+value+y << endl;
Is this possible in C++?
Thanks
As already mentioned, you cannot somehow "build" some string at run time and use it as variable name. This is a compile time mechanism, and even if this was a compile time problem, it would be a bad idea.
You most likely want
std::vector<int> nx(someLength);
std::vector<int> ny(someLenght);
int value = 5;
cerr << nx[value] << ny[value] << endl;
instead. (Or the same thing with std::array<int, someLength> nx{} if someLength is known at compile time and not big.)
cerr << n+value+x << n+value+y << endl;
Uhhm what please? You mean to expand symbol names and bind to them at runtime?
No that's not possible.
The closest thing you can get is setting up a std::map<std::string,int> and generate the key string values as needed.

C++ get variable from variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there another solution, please?
if(!http_piconpath && http_tpl)
{ http_piconpath = http_tpl; }
If not exist http_piconpath but exist http_tpl then assign the value from http_tpl to http_piconpath.
You provide very little information about what you are doing. Assuming that you use strings from your comment, the if statement you've got is not valid for strings, you'll see your compiler screaming that it can't convert string to bool. Below is a very basic example. Note that you must initialise http_piconpath, else it will have a garbage value and you won't know if its value is set or not.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string http_piconpath = "";
string http_tpl = "string";
if(http_piconpath == "" && http_tpl != "") {
http_piconpath = http_tpl;
}
cout << http_piconpath << endl;
return 0;
}
Supposing that both are pointers (of compatible types),
if(!http_piconpath) http_piconpath = http_tpl;
Or
http_piconpath = http_piconpath ? http_piconpath : http_tpl;
If picon is null, it gets the value of tpl; if both are null, nothing changes.