What would be an easy way to construct a vector triplet of ints in CPP?
i.e instead of a pair of 2 ints ,
std::vector<std::pair<int, int> > vec;
I want 3 int's tied together as one element of the vector.
I realized one way would be to make 2 sub-nested pairs, but this method gets messy. I am not aware of all the details of CPP, therefore please recommend an easier way if available. Thank you.
std::vector<std::tuple<int,int,int>> myvec;
No need to over-engineer.
struct Triplet
{
int one_, two_, three_;
};
vector<Triplet> triplets;
Check out boost tuple http://www.boost.org/doc/libs/1_49_0/libs/tuple/doc/tuple_users_guide.html
You can easily create Pairs, triples, quadruples, up to n-uples!
In C++11, there is std::array, see here. In C++03, I would probably define a struct of 3 ints and make a vector of those.
Related
Haven't worked with vectors very much, so please excuse my ignorance. I am looking to have a vector of size 10 that can hold multiple instances of a class that I have within the SAME element of the vector. so say for example
my_vector[3] = my_obj1 and my_obj2
is this even possible? would I need something like a 2d vector to implement this? is there a better solution to my problem? I don't have any code to try so please don't say "where is your code and errors?"
You can just make a vector of vectors.
std::vector<std::vector<MyObj>> vectorOfVectors;
std::vector<MyObj> tempVector;
tempVector.push_back(my_obj1);
tempVector.push_back(my_obj2);
vectorOfVectors.push_back(tempVector);
I am trying to write a small banlist with std::vector/std::map. But i don't know how it should work yet...
Here is how "BanList" is build on Networking.h:
static std::vector<int, std::string>BanList;
Int is for the ID
string for the target IP
This is a snippet of my Networking.cpp (Where the target gets added to the banlist)
if (boost::contains(dataPackage.data, needle1) && boost::contains(dataPackage.data, needle2))
{
// All okay here - Let's jump over & let the thread handle the action
}
else
{
//e.g. BanList.addTarget(Auto-Incremented ID, TargetsIP);
break;
}
So there on the line where's // e.g BanList.addTarget(int, string); how should it work with std::vector or std::map? How can i create now a list full of the targets? To get the IP is not my problem! The problem is how to set the ID automatically and add then the target to the list... Already now thank you for your help.
I'm not quite sure what your question is here. If you want to know how to use maps, then you should see the the online reference.
In your particular case, if you use the map:
static std::map<int, std::string> banList;
banList[id] = ipAddress;
I don't know why you would want to map ints to strings for a ban list. But this is how you do it.
For a vector, you can't have key/value pairs unless you're pushing a std::pair object. For that though, you almost always will want to use a map.
To add to a vector, use vec.push_back(item).
You can find almost all of this on online references.
Carefuly read the std::vector's template parameters. std::string is not a valid allocator for int :)
This would be closer to std::map<int, std::string>:
std::vector<std::pair<int, std::string>> BanList;
Learn the rest about std::vector/std::pair or std::map from the reference/your favorite book. It isn't worth explaining it here (and there is not enough space for it).
If TargetsIP is something like std::vector<std::string>, you will need to iterate through it and append elements to BanList in a loop.
map<string ,vector<string> > hashes;
hashes.insert(pair<string,vector<string> > (a,b )); //error coming
What is the problem coming when i am using the above statement in C++, where a and b are strings?
How does the insertion takes place in this type (i.e. one container containing more container) of associative container?
Many many thanx in advance
You probably want
hashes[a].push_back(b)
That's if you want b to be appended to the present vector. If you want it to replace it, use
hashes[a].assign(1, b)
You're trying to use a string where a vector of string is needed. You need to insert b into a vector, then insert (a, your_vector) into the map.
Alternatively, use a multimap<string, string> to get the same basic effect in a way you may find easier to use. This would allow your insert(pair<...>(a, b)).
Also consider using std::make_pair instead of instantiating std::pair directly. It'll deduce the types for the arguments so you don't need to fill them out explicitly.
Just follow the definition:
vector<string> v;
v.push_back("mystring");
hashes.insert(std::make_pair("key", v));
Note how the second parameter to make_pair is a vector<string>. This will never fail then. It will fail if the second parameter is a string.
I have a problem with the creation of a hash of arrays. I need a Single Key - Multi Data system:
multimap <Type, vector<type> > var;
But how I can add elements to the vector?
Example: key = 3;
Now I need to append some elements into the vector whose key is 3.
Creating a temp-vector not an answer because I don't know when I need to input element into the vector with the current key.
sorry, understand my problem. i need fast-access struct, that will be operate with ~50,000 words with length ~20 each.
and i need something like tree.
also, have question:
how quick STL-structures, like vector,map,multimap and other?
What's wrong with std::map <KeyType, std::vector<SomeType> >, or some other collection as the value type? This gives you control over how to operate on the value collection. A multimap to me seems like a low-level form of std::map <KeyType, std::list<SomeType> >, but with none of the flexibility of a list.
To find the answer to your question you can look at the slides under point 6. at this site https://ece.uwaterloo.ca/~ece250/Lectures/Slides/
Hope that helps!
Is it okay to initialize a 2D vector like this (here all values in a 5x4 2D vectro are initialized to 3)?
std::vector<std::vector<int> > foo(5, std::vector<int>(4, 3));
This seems to behave okay, but everywhere I look on the web people seem to recommend initializing such a vector with for loops and push_back(). I was initially afraid that all rows here would point to the same vector, but that doesn't seem to be the case. Am I missing something?
This is perfectly valid - You'll get a 2D vector ([5, 4] elements) with every element initialized to 3.
For most other cases (where you e.g. want different values in different elements) you cannot use any one-liner - and therefore need loops.
Well, the code is valid and it indeed does what you want it to do (assuming I understood your intent correctly).
However, doing it that way is generally inefficient (at least in the current version of the language/library). The above initialization creates a temporary vector and then initializes individual sub-vectors by copying the original. That can be rather inefficient. For this reason in many cases it is preferrable to construct the top-level vector by itself
std::vector<std::vector<int> > foo(5);
and then iterate over it and build its individual sub-vectors in-place by doing something like
foo[i].resize(4, 3);