hey guys i need some help immediately...
i am usually using c# but have to make a code in c++ so was quickly going through the useful datatypes and procedures
Here's the code:
#include<iostream>
#include <unordered_map>
#include <vector>
#include <string>
using namespace std;
void insertInHashtable(string customerString,unordered_map<string, string> &hashtable )
{
string customerPurchaseArray, name;
int i= 0, firstCommaPosition = 0;
int length = customerString.length();
while (i<length)
if (customerString[i] == ',')
{
firstCommaPosition = i;
break;
}
else
i++;
customerPurchaseArray.assign(customerString, firstCommaPosition + 1, string::npos);
name.assign(customerString, 0, firstCommaPosition - 1);
hashtable.insert(name, customerPurchaseArray);
}
int main (int args[])
{
string value = " error...!!!";
unordered_map<string, string> hashtable;
string customerString = "Ayush,p1234,p345,p34,p43,p444";
insertInHashtable(customerString, hashtable);
unordered_map<string, string>::iterator got = hashtable.find("Ayush");
if (got != hashtable.end())
value = got->second;
std::cout<<value;
char ch;
std::cin>>ch;
}
when i got stuck at this issue..
here im trying to use unordered_map<string, string> but im getting a series of errors which i dont really get like :
Error 1 error C2039: 'iterator_category' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 373 1 CPPTP
and 5 others...
as i only got to know about these functions an hour ago im presuming that its just wrong usage or call by reference is not rite...
so does anyone have any idea what the problem is and how to solve it... any advice will be appreciated...
Use either:
hashtable.insert(make_pair(name, customerPurchaseArray));
Or:
hashtable.emplace(name, customerPurchaseArray);
Or:
hashtable[name] = customerPurchaseArray;
Note that there's a difference: The first two will not change any existing elements, whereas the last one always, unconditionally overwrites existing elements.
Related
I would ask for your help and apologise if the question doesn't make sense.
I have a Microsoft Visual Studio project which I want to execute it multiple times in one go, and every time I will change one parameter.
Please see below the concept:
#include <iostream>
using namespace std;
int size_list = 2;
int my_list[2] = { 5, 6 };
int main()
{
for (int i = 0; i < size_list; i++)
{
cout<<"the number is "<<my_list[i]<<endl;
}
return 0;
}
So, I would like to replace the loop and instead I will have each element of my_list as parameters.
Is there any way to do so?
Thanks
You can pass the parameters in main().
int main(int argc, char* argv[]){std::cout<< argv[1];}
And use script like
for ($i=0;$i -lt 5; $i++)
{
#your CPP project and parameters array
Start-Process test.exe $arr[0]
Wait-Process test
}
Write a function partlist that gives all the ways to divide a list (an array) of at least two elements into two non-empty parts.
From what I understand, the function should produce linear partitions (using term as mathematical) of the original array.
I think I understand each of the types for the function individually, but I am struggling to bring them all together.
(I have 6 months of C++ experience and no other languages. This is an exercise from codewars that I'm using to try to improve my coding skills)
I've written the function code up to the point where I want to start testing, but with the way the problem is worded, I do not understand how to instantiate the class type. I've reviewed statics, vectors, pairs, and constants in individual terms from class notes and cplusplus.com.
I've gotten to the point that the program will compile, but will not complete main(). I feel like I'm missing a vital bit of information, and I appreciate any help to understand the goal of the program.
#include <iostream>
#include <vector>
class PartList
{
public:
static std::vector<std::pair <std::string, std::string>> partlist(std::vector<std::string> &arr);
};
///The above is what I have to work with///
int main(){
std::vector<std::string> tester = {"I", "Love", "To", "Discrete"};
PartList::partlist(tester);
}
std::vector<std::pair<std::string,std::string>> PartList::partlist(std::vector<std::string> &arr){
std::vector<std::pair<std::string,std::string>> output;
std::vector<std::pair<std::string,std::string>>::iterator bigIt = output.begin();
std::vector<std::string>::iterator myIt;
for(std::vector<std::string>::iterator secIt = arr.begin();
secIt != arr.end(); secIt++){
myIt = arr.begin();
while(myIt <= secIt){
bigIt->first += *myIt;
myIt++;
}
while((myIt > secIt) && myIt != arr.end()){
bigIt->second += *myIt;
myIt++;
}
}
return output;
}
Expected:
For set {std::string a, std::string b, std::string c, std::string d}
Should result in {a, bcd}, {ab,cd}, {abc,d}
Result:
nothing
As john said in comments. you're not actually doing anything with output. At the beginning of your for loop, you need to append a new item to output with output.push_back(). Then, instead of using an iterator, just reference that item using output.back()
code:
using std::vector;
using std::string;
using std::pair;
vector<pair<string, string>> PartList::partlist(vector<string> &arr)
{
vector<pair<string, string>> output;
vector<string>::const_iterator arr_iterator;
for (vector<string>::const_iterator secIt = arr.begin(); secIt != std::prev(arr.end()); secIt++)
{
arr_iterator = arr.begin();
output.push_back(pair<string, string>());
while (arr_iterator <= secIt)
{
output.back().first += *(arr_iterator++);
}
while (arr_iterator != arr.end())
{
output.back().second += *(arr_iterator++);
}
}
return output;
}
include "stdafx.h"
#include <vector>
#include <iostream>
std::vector<int> biggest;
std::vector<int>vector1;
std::vector<int>vector2;
int main(){
biggest = [vector2[0],0]; //wrong initialization
for (int apply = 0; apply < (vector2.size()); apply++) {
if (biggest[0] < vector2[apply + 1]) {
biggest[0] = vector2[apply + 1];
biggest[1] = apply + 1;
}
}
Error C2065 'apply': undeclared identifier.why this error is occurring as i have already defined apply variable in for loop. error should be in initialization of biggest(vector).why wrong compiler code?
even intellisense is not giving me error is it a visual studio bug?
apply is in scope in the for loop body, so be assured, the error is not there. But you are aware that apply is out of scope after the loop body?
I'm only answering this because your use of
vector2.size() - 1
will give you hell if vector2 is empty, as the above will wrap around to a large unsigned value and your program will fail spectacularly! Use
for (int apply=0; apply + 1 < vector2.size(); apply++) {
instead.
I'm programming in c++ in Visual Studio.
This is my subroutine, which gives me a specific value back from a double array:
double specific_value_search(std::string mol_fractions_name[], std::string mass_fractions_name_output[], double mass_fractions_output[], int molecule)
{
double specific_value=5;
std::string a = mol_fractions_name[molecule];
std::string b;
for (int i = 0; i <= 11; i++)
{
b = mass_fractions_name_output[i];
if (a.compare(b) == 0)
//if ((a.find(b) != std::string::npos))...this was my second try // sollte string b in Zeile a gefunden werden, dann...
{
specific_value = mass_fractions_output[i];
break;
}
}
return specific_value;
}
so when I execute this code in my project to an .exe, the code runs fine.
but when I compile it to an dll, an run it via an external program, the value returns 5, because of my initalisation (without initialisation the program crashes because of trying to return an uninitialized variable.
I added the values from visual studio in the screenshots below
Does anyone have any advice?
Screenshot 1 - values from visual studio
Screenshot 2 - values from visual studio
If you can use a standard container (std::map or std::unsorted_map) then this becomes trivial.
std::map<std::string, double> fractionNames;
// fill map
double specificValue = fractionNames[mol_fractions_name[molecule]];
If it's possible that molecule is bigger than the number of names or that you need to generate an error if the fraction name isn't found in the map then you'll have to add some code to detect and handle these situations.
If you can't use a map, then you could use a vector
struct FractionName {
std::string name;
double value;
}
typedef std::vector<FractionName> FractionNameVector;
FractionNameVector fractionNames;
// again fill fractionNames
FractionNameVector::iterator iter = std::find(fractionNames.begin(), fractionNames.end(), SearchPredicate(mol_fractions_name[molecule]));
This will need a SearchPredicate like this
struct SearchPredicate
{
bool operator()(const FractionName& haystack) { return haystack.name ==
needle; }
explicit SearchPredicate(const std::string name) : needle(name) {}
std::string needle;
};
You could use a lambda if you are using C++11 or later.
I just implemented breadth first search in c++ and instead of declaring a vector as bool, I declared it as an int. This lead to a very odd observation. When I used int, the code printed the following:
1
32763
-524268732
Throughout the entire code, I don't provide any such value to variable as the 2nd and 3rd node receive, so I assume that they are just garbage values, but why do garbage values even come up, when I'm initialising the vector to be full of zeroes ??? You may check the code to be that below:
#include <iostream>
#include <queue>
using namespace std;
queue<int> neigh;
vector< vector<int> > graph(3);
vector<int> flag(3, 0);
int main(void)
{
graph[0].push_back(1); graph[0].push_back(2);
graph[1].push_back(0); graph[1].push_back(2);
graph[2].push_back(0); graph[3].push_back(1);
neigh.push(0);
while(!neigh.empty())
{
int cur = neigh.front();
neigh.pop();
flag[cur] = 1;
for(int i = 0, l = graph[cur].size();i < l;i++)
{
if(!flag[graph[cur][i]])
neigh.push(graph[cur][i]);
}
}
for(int i = 0;i < 3;i++)
{
cout << flag[i] << endl;
}
}
Alright, then I changed just a single line of code, line number 7, the one where I declare and initialise the flag vector.
Before:
vector<int> flag(3, 0);
After:
vector<bool> flag(3, false);
And voila! The code started working:
1 //The new output
1
1
So, my question is, what is the problem with the code in the first place ? I believe it may be some kind of error I made, or possibly that its only by chance that my bfs implementation works at all... So, what is the truth, SO? What is my (possible) mistake ?
You are accessing your vector out of bounds here:
graph[3].push_back(1);
At this moment, graph only has three elements. This leads to undefined behaviour.