So in my cpp file I'm trying to declare a map as follows:
map<string, vector<myStruct>> myMap;
At the top of my file I have written using namespace std and I also have #include <string>
.
However I'm getting these weird errors:
error: ISO C++ forbids declaration of ‘map’ with no type
I don't know how to fix it. If I write #include <map> that just causes the compiler to freak out.
do you have #include <map>? rest looks valid,
however you might need to add a space if your C++ standard is not C++11:
#include <map>
#include <vector>
#include <string>
using namespace std;
map<string, vector<myStruct> > myMap;
^^^
even better not use namespace std:
#include <map>
#include <vector>
#include <string>
std::map<std::string, std::vector<myStruct> > myMap;
You need to include map header file.
#include <map>
Meanwhile, in case you are not using C++11, you need a space:
map<string, vector<myStruct> > myMap;
//^^
You should also include <map>. std::map is introduced through this header.
Furthermore, using namespace std is considered a bad practice. You should either have a using statement or use the prefix the name with std:: to denote a fully-qualified identifier:
#include <map>
#include <string>
#include <vector>
std::map<std::string, std::vector<myStruct>> myMap;
Note, the lack of a using statement ;)
#include <vector>
#include <string>
#include <map>
#include <iostream>
typedef int myStruct;
std::map<std::string, std::vector<myStruct>> myMap;
int
main()
{
std::vector<myStruct> testMe = { 1, 2, 3};
myMap["myTest"] = testMe;
std::cout << myMap.size() << std::endl;
return(0);
}
Related
#include <iostream>
#include <algorithm>
#include <climits>
#include <map>
#include <unordered_map>
using namespace std;
int main()
{
std::map<int, std::unordered_map<std::pair<int, int>, int>> region;
region[0].insert(make_pair(make_pair(1, 1), 1));
return 0;
}
I am writing the above code and it don't work as expected, How can I fixed it? The error is " error C2064: term does not evaluate to a function taking 1 arguments"
There is no specialization of std::hash for std::pair, so it can't be used as a key for std::unordered_map unless you provide a custom hash function.
I use QT creator and Xcode 4.63 (osx mav)
I just call .set(....) and got following error
no member named 'set' in 'std::vector<int, std::allocator<int> >
seem like something wrong with lib , since I'm new to QT creator i still got no idea how to make it right. (I'm new to c++)
#include <cctype>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "console.h"
#include "filelib.h"
#include "grid.h"
#include "gwindow.h"
#include "simpio.h"
#include "vector.h"
using namespace std;
int main() {
vector<int> myVector(10,0);
fstream mystream;
string getStream;
while(getline(mystream,getStream)) {
getline(mystream,getStream);
if(stringToInteger(getStream)>=0 && stringToInteger(getStream)<=9) {
myVector.set(0,(myVector[0]+1))
};
}
return 0;
};
The "set" method doesn't exist for vectors in STL. You could use the [] operator if you want to change a value:
myVector[0] = myVector[0] + 1;
or even
myVector[0]++;
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.
sorry if this is a newB question,
please conider the following code:
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/tokenizer.hpp>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
using boost::multi_index_container;
using namespace boost::multi_index;
typedef multi_index_container<
std::string,
indexed_by<
sequenced<>,
ordered_non_unique<identity<std::string> >
>
> text_container;
typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;
int main()
{
std::string text=
"Alice was getting very tired of sitting by her sister";
text_container tc;
text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-"));
std::copy(tok.begin(),tok.end(),std::back_inserter(tc));
int i=0;
for(text_container::iterator bb=tc.begin();bb!=tc.end();bb++,i++)
// std::cout << *bb << std::endl;
std::cout << tc[i] << std::endl;
return 0;
}
I would like to access, for example, the 10th element in the container. do I still have to use an iterator? or is there away to access a specific sequenced element in array-like fashion(or any other way...please suggest)
Appreciate your help
vahid
You can specify a random access index for the multi index by changing the line sequenced<>, to random_access<>, (you'll need to #include <boost/multi_index/random_access_index.hpp>). This will allow you to remove the iterator in the for loop.
For further details, see the documentation.
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;