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.
Related
I am trying to use boost::container::map. During inserting data, the error "insert is ambiguous" is shown.
#include <boost/container/map.hpp>
#include <string>
#include <iostream>
int main()
{
boost::container::map<std::string, int> map;
map.insert("Ram",0);
}
Your style of inserting is not correct. I provide the code:
#include <boost/container/map.hpp>
#include <string>
#include <iostream>
#include <ostream>
int main()
{
boost::container::map<std::string, int> map;
map.insert(std::pair<const std::string, int>("Ram",1));
std::cout<< map["Ram"];
return 0;
}
The following gives an error for the code mentioned below.
Where I have gone wrong ?
error: ‘function’ is not a member of ‘std’
I want to make priority queue using C++ std lib queue, and min the queue is that IceCream which takes least time to prep. I have tried implementing this -> declaring a priority_queue in c++ with a custom comparator
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <iterator>
#include <string>
#include <sstream>
using namespace std;
class IceCream
{
public:
int time_to_prep;
IceCream(int flav) {
time_to_prep = flav;
}
};
bool Compare(IceCream a, IceCream b)
{
return a.time_to_prep > b.time_to_prep;
}
int main()
{
priority_queue<IceCream, vector<IceCream>, std::function<bool(IceCream a, IceCream b)> > my_pq(Compare);
my_pq.push(IceCream(4));
my_pq.push(IceCream(33));
my_pq.push(IceCream(9));
cout << my_pq.top() << endl;
return 0;
}
#include <functional>
You need this include to get access to std::function
See: http://en.cppreference.com/w/cpp/utility/functional/function
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.
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);
}
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;