I want to fetch value of certain key from back-end.
In back-end the structures are defined and values are initialized.
Consider this as structure defined in back-end:
struct person{
string nme;
string adrs;
int id;
};
person p1 = {"steve","ABC street",23};
The key address corresponds to value of p1.adrs in back-end.
Now the key address is to be mapped to (p1 & adrs) in external file and should get the value "ABC street".
My question is how mapping is to be done for key and its particular structure member in external file and how to fetch value for that key.
Here Can I use std::map concept for this?
I got solution for requirement. But to improve it further, mapping is to be declared in separate file (Here it was declared in .h file)
myfile.h
#ifndef MYFILE_H
#define MYFILE_H
#include <iostream>
#include <algorithm>
#include<vector>
#include <string>
#include <map>
using namespace std;
struct Chassis
{
string InLED;
string AstTg;
};
struct Manager
{
string MngrType;
int count;
};
const Chassis chassis1={"On","null"};
const Manager manager1={"BMC",23};
const map<string, const string>cha1={{"IndicatorLED", chassis1.InLED},{"AssetTag",chassis1.AstTg},{"ManagerType",manager1.MngrType}};
const map<string, int>cha2={{"Count",manager1.count}};
void func(string);
#endif
myfile.cpp
#include <iostream>
#include <string>
#include "myfile.h"
#include <map>
using namespace std;
void func(string item)
{ if(cha1.find(item) == cha1.end()) {if (cha2.find(item) == cha2.end()){} else {cout<< item<<":"<<cha2.at(item)<<endl;} }
else {cout<< item<<":"<<cha1.at(item)<<endl;}
}
main.cpp
#include <iostream>
#include <string>
using namespace std;
#include "myfile.h"
int main() {
string item="IndicatorLED";
func (item);
string item1="AssetTag";
func(item1);
string item2="ManagerType";
func(item2);
string item3="Count";
func(item3);
}
Related
I would like to implement a hash table example.
So for this aim, I have created one header, one hash.cpp and main.cpp files.
in my hash.cpp , I tried to run a dummy hash function which takes key value and turns into an index value. however, it throws an error(reference to 'hash' is ambiguous) whenever I try to create an object according to that hash class.
this is my main.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int main(int argc, const char * argv[]) {
hash hash_object;
int index;
index=hash_object.hash("patrickkluivert");
cout<<"index="<<index<<endl;
return 0;
}
this is my hash.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int hash(string key){
int hash=0;
int index;
index=key.length();
return index;
}
this is my hash.h
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef __hashtable__hash__
#define __hashtable__hash__
class hash
{
public:
int Hash(string key);
};
#endif /* defined(__hashtable__hash__) */
Your hash class symbol is clashing with std::hash
A quick fix could be using a global namespace qualifier
int main(int argc, const char * argv[]) {
::hash hash_object;
but a better and recommended one would be to stop polluting your global namespace with
using namespace std;
and just using std::cout or std::endl when you need them.
You could also create your own namespace in case you're writing a library.
Besides, you have some capital letter typos here:
index = hash_object.hash("patrickkluivert");
^ I suppose you're referring to the Hash() function here
and here
int Hash(std::string key) {
^ this needs to be capital as well
int hash = 0;
in case you want to match your declaration and avoid cast/linking errors.
Your hash class is conflicting with std::hash. Stop using using namespace std; right now. If you want to make print statements shorter, try using std::cout; using std::endl;
I have this class (hashMap.h):
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include "functions.h"
using std::cout;
using std::vector;
using std::endl;
using std::string;
class hashMap
{
public:
explicit hashMap(int hashEntrySize = 101) : hashVector(nextPrime(2 * hashEntrySize)), currentSize{ 0 }
{}
bool containsKey(const string & searchKey);
bool containsVector(const vector<string> searchVector);
void insert(const string & keyTarget, const vector<string> & insertVector);
void insertAfterReHash(const string & keyTarget, const vector<string> & insertVector);
int getCurrentSize() const;
void assignKey(string & newKey);
private:
enum EntryType { ACTIVE, EMPTY, DELETED };
struct hashEntry
{
vector<string> vectorValue;
EntryType status;
int keyID;
string key;
hashEntry(EntryType s = EMPTY)
:status(s), keyID{ -1 } {}
};
size_t hashFunction(const string & key);
bool isActive(int currentPos) const;
int findPos(const string & keyTarget);
void reHash();
vector<hashEntry> hashVector;
int currentSize;
};
And a function header file (functions.h):
#pragma once
#include <iostream>>
#include <vector>
#include <string>
#include <fstream>
using std::string;
using std::cout;
using std::vector;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
hashMap computeAdjacentWords(const vector<string> & words) //error at this line
{
hashMap hm(500);
//do stuff with object
return hm;
}
And the main file:
#include <iostream>>
#include <vector>
#include <string>
#include <fstream>
#include "hashMap.h"
using std::string;
using std::cout;
using std::vector;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
int main()
{
vector<string> words;
string line;
ifstream dictionaryFile;
dictionaryFile.open("largedictionary.txt");
words = readinWords(dictionaryFile);
dictionaryFile.close();
hashMap hm = computeAdjacentWords(words);
return 0;
}
I created the hashMap class and I want to be able to return a hashMap object, but this is giving me an error of "Error C4430 missing type specifier - int assumed." What am I doing wrong?
I put the code in files and nicely asked the compiler to do its job. This is the first warning from the list:
$ cc main.cpp -c
In file included from main.cpp:5:
In file included from ./hashMap.h:6:
./functions.h:16:1: error: unknown type name 'hashMap'
hashMap computeAdjacentWords(const vector<string> & words) //error at this line
^
The compiler doesn't know what hashMap is. When it reaches the line with the error, the hashMap symbol was not yet declared or defined.
You shouldn't define functions in header files.
Rename functions.h to functions.cpp, add #include "functions.h" at the end of the list of includes.
Create a new file functions.h that contains only the declarations of the functions (the function header) and the types they use:
#ifndef __FUNCTIONS_H__
#define __FUNCTIONS_H__
#pragma once
//#include <iostream>
#include <vector>
#include <string>
//#include <fstream>
#include "hashMap.h"
using std::string;
using std::vector;
// Do you really need all these types here?
using std::cout;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
hashMap computeAdjacentWords(const vector<string> & words);
#endif // __FUNCTIONS_H__
You are including functions.h from hashmap.h BEFORE the hashMap class is defined. As such, when the compiler reads functions.h, the hashMap class is not defined.
i am trying to find the common elements(song_list) between two vectors, in my user.h file i have two vectors and am trying to use this vectors in another class(help.cpp) but i cant get it right. please whats wrong with my code? it doesn't run and am not entirely sure if this is the right approach.
user.h
#pragma once
#include <string>
#include <vector>
#include <string>
using namespace std;
class User
{
public:
User(void);
~User(void);
public:
struct user1
{
string name;
int age;
string song_list;
};
typedef vector <user1> u1;
struct user2
{
string name;
int age;
string song_list;
};
typedef vector <user2> u2;
};
help.cpp
#include "Help.h"
#include "User.h"
#include <iterator>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
int commonElements();
int commonElements(){
vector <user1> u1;
vector <user2> u2;
std::sort(u1.begin(), u1.end());
std::sort(u2.begin(), u2.end());
std::vector<string> common;
std::set_intersection(u1.begin(), u1.end(), u2.begin(), u2.end(),
std::back_inserter(common));
cout<<common<<endl;
}
thats my code so far. All data for the user's name,age and song list is on a .txt file.
You seem to be somewhat confused about types vs. instances. You're creating some separate types where (I'm pretty sure) you just want two instances. For example, it appears (to me) that you really want u1 and u2 to be vectors of the same type of objects.
user.h:
#pragma once
#include <string>
#include <vector>
#include <string>
using namespace std;
struct user {
string name;
int age;
vector<string> song_list;
};
help.cpp:
#include "Help.h"
#include "User.h"
#include <iterator>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
int commonElements(user &u1, user &u2) {
sort(u1.song_list.begin(), u1.song_list.end());
sort(u2.song_list.begin(), u2.song_list.end());
vector<string> common;
set_intersection(u1.song_list.begin(), u1.song_list.end(),
u2.song_list.begin(), u2.song_list.end(),
back_inserter(common));
for (auto const &song : common)
cout << song << "\n";
#if 0
// alternatively, write the intersection directly to the stream:
set_intersection(u1.song_list.begin(), u1.song_list.end(),
u2.song_list.begin(), u2.song_list.end(),
ostream_iterator<string>(cout, "\n"));
#endif
}
As an aside, it's generally agreed that putting a using namespace std; in a header is a really lousy idea. I've left it because it's mostly unrelated to the question(s) at hand, but it should really be changed.
error C2071: 'Lexicon::list' : illegal storage class
I have a class that reads a bunch of strings into memory and then provides functions that allow applying operations on those strings and their relationships. As part of this I'd like to have a shared memory between the main.cpp where some of the operations are initiated and the class where the operations are completed. For this, in a previous post, it was suggested to use an extern type. But, now there is an error. How do I resolve this error and have a memory space shared by several classes?
in lexicon.h
#ifndef _lexicon_h
#define _lexicon_h
#include <string>
#include <vector>
using namespace std;
class Lexicon {
public:
Lexicon();
~Lexicon();
extern vector<vector<string>> list;
void buildVectorFromFile(string filename, vector<vector<string>> &list, int v, int h);
private:
struct charT { char letter; nodeT *next;};
};
#endif
in main.cpp
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include "lexicon.h"
void buildVectorFromFileHelper (Lexicon & lex)
{
vector<vector<string>> list;
lex.buildVectorFromFile("ASCII.csv", list, 200, 2); //build 2x200 vector list
}
Ok, I missunderstood your previous question (this is what happens when you don't post full code). Inside a class, extern is not used:
in lexicon.h
#ifndef _lexicon_h
#define _lexicon_h
#include <string>
#include <vector>
using namespace std;
class Lexicon {
public:
Lexicon();
~Lexicon();
vector<vector<string>> list;
private:
struct charT { char letter; nodeT *next;};
};
#endif
in main.cpp
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include "lexicon.h"
void buildVectorFromFileHelper (Lexicon & lex)
{
vector<vector<string>> list;
lex.buildVectorFromFile("ASCII.csv", list, 200, 2); //build 2x200 vector list
}
The problem here is that Lexicon doesn't have the method buildVectorFromFile, so how are you calling lex.buildVectorFromFile("ASCII.csv", list, 200, 2);?
To share the same vector, if it's a member, make it static:
class Lexicon {
public:
Lexicon();
~Lexicon();
static vector<vector<string>> list;
private:
struct charT { char letter; nodeT *next;};
};
In lexicon.cpp:
vector<vector<string>> Lexicon::list;
The rules of an extern memory is explained here in this daniweb thread; the comment there is that yes, this should be simple but it is somehow not intuitive. The gist is that the memory is globally declared with the extern prefix in .cpp file A and then to reuse the memory in cpp B, globally declare it again in .cpp file B.
I think Luchian_Grigore and #jahhaj were getting there but we had either just not found the words for me to understand or they were still finding the words to explain.
Simple question....
I have a header file called bag...
#include <map>
#include <iostream>
#include <string>
using namespace std;
class Bag
{
public:
Bag();
map <const string str, int number> items;
private:
};
#endif
In the implementation, I'd like to insert something into bag:
#include <string>
#include <fstream>
#include <map>
#include "Bag.h"
using namespace std;
Bag::Bag()
{
items["brush"] = 4;
}
But for some reason, i can't access items. What am I doing wrong????
Thanks!
Duh! For some reason I'm trying to insert a value name with the value type field. Thanks Chad, I think you fixed me!
map <const string, int> items;