Find an element in a vector array [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find an item in a std::vector?
I am using C++ Builder to create a VCL Forms application. I also have a vector array of appointment objects that each have a name, type, reminder date/time, a date/time, location and comments.
I am wanting to implement a find feature that will let a user find an appointment given certain criteria.
The user can choose to find an appointment in the vector array by either choosing the name, type etc or a combination of each.
What would be the best programming concept to use for this situation? The vector is not large. No more than 10 or 20 elements.
Thanks

Use std::find_if() and define the required predicate (if C++11 you can use lambda function).
See online demo http://ideone.com/Md7sp.

std::find_if(A.begin(),A.end(),isthatit(conditions));
where isthatit is a predicate object constructed from conditions.

If you have many criterias you should think about
Boost.MultiIndex container which targets different search indexes.
http://www.boost.org/doc/libs/1_51_0/libs/multi_index/doc/tutorial/index.html

Related

Is there a built-in function that will shuffle an array? [duplicate]

This question already has answers here:
Is it possible to random_shuffle an array of int elements?
(5 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I am trying to create a deck of cards in C++. I store that deck in an array array<Card, 52> deckOfCards = generateCards(). Once I create the array, is there any way to shuffle the objects inside of it easily? For example, in Java, you could use the shuffle() algorithm and the objects would be shuffled.
People have mentioned the usage of random_shuffle in the comment, however random_shuffle has been deprecated since C++14, and removed since C++17.
Yes, C++ does have a shuffle() function. However, you must also add a random generator in there manually, which could be daunting at a first glance. However, if you don't really care the exact generator and engine being used, then this could be done simply with:
std::shuffle(deckOfCards.begin(), deckOfCards.end(), std::default_random_engine{/*optional seed*/});
or
std::shuffle(deckOfCards.begin(), deckOfCards.end(), std::random_device{});
Where the first one is deterministic and the second one is non-deterministic.

Use of multisets in C++ [duplicate]

This question already has answers here:
Give me a practical use-case of Multi-set
(4 answers)
"multiset" & "multimap" - What's the point?
(7 answers)
Closed 2 years ago.
I understand the usage on sets in C++, but why do multisets exist?
What are some real world applications where multisets are useful?
This argument can extended for unordered multisets as well, what differentiates then from using a vector and what advantages and disadvantages does it provide?
Because you don't have to store single-element objects in a multi-set. You're thinking of storing something like a string in a multi-set. But that's not what it's made for. You can have any struct you want, and make the comparison be with a single element in the struct.
For example:
struct PhoneBookEntry
{
std::string name;
std::string phoneNumber;
}
In this naive "phone book" entry, there's no reason to have a single entry per name in a phone book. There might be many. So you make a multiset of PhoneBookEntry, and you make the comparator be by name. This way, you can have multiple phone numbers with the same name.
Now you might think that a map is more suitable for this, sure. But this is just an example. If you have a structure where you don't need a key/value but you need the search properties of a set with multiple elements per key, you use a multiset.

Which datastructure to use for finding an element [duplicate]

This question already has answers here:
How can I efficiently select a Standard Library container in C++11?
(4 answers)
Closed 4 years ago.
I have several items saved in a list. I would like to add items that have already been processed to a datastructure (this makes sense in my case even though you might wonder why). When processing the next item from the list I first want to make sure if it has been processed before so lets say something like this:
if(element_is_in_datastructure(current_element)) {
do this
}
else
{
do that
add_element_to_datastructure(current_element)
}
My question is, what is the ideal datastructure where checking if the element is in it won't take too long. At the moment I don't have too many elements (max 30) which will be added to the datastructure, but this number might increase and I don't want to lose performance.
You can use a map e.g std::unordered_map to store your elements as keys.
Then just check their presence e.g
if(!yourMap.count(element))
{
// your element is not in the structure
}
This finding takes logarithmic time in the map's size to finish.

is there any way to let the user choose which variable is to be inputted? [duplicate]

This question already has answers here:
Accessing a class member variable by its name at runtime [duplicate]
(3 answers)
Closed 5 years ago.
this is for a personal use application in c++
for example:
class x
{...};
int main()
{
string userinput;
cin>>userinput;
cin>>x."userinput";}
is something like this possible?
If what you are talking about is allowing for the user to set variable names at runtime, then no. It is not possible in c++. Depending on what you are trying to achieve, there are a few ways around this.
If you just need the user to be able to access a small number of variables, all of which are known before hand, you can use a switch statement, or a bunch of if/elseifs
If you need to store a larger amount of variables the number of which may not be known at compile time, then there exists a number of data structures that exist just for this purpose. There are arrays, hash tables, linked lists and hundreds of variations on the above. These are all far too complex topics to cover in a single answer however.

vector operations in c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Open source C++ library for vector mathematics
I've to do a very simple question: how can I do basic operations like sum, difference or product on two diffent int vector, like in matlab, using c++? does exist any function that can do it?
Thanks in advance.
Not in the standard library, you will have to use a third party library, I don't know what your requirements are, but you could take a look at something like boost::ublas.
Take a look at the standard algorithms: http://en.cppreference.com/w/cpp/algorithm
Depending on your requirements (which you didn't really elaborate on), you might be looking for anything from std::for_each to Boost::uBLAS...
You can write you own class, like class Vector, class Matrix, where you overload the operators like +, -, *. Or you can use libraries like LAPACK, boost ublas, ...
Use std::accumulate to accumulate a single value e.g. the total sum or total product.
Use std::inner_product to generate a value which is the result of a binary operator between values in 2 vectors, and a binary operation between successive results. This is a very useful function, if you can formulate your problem correctly. This is related to MapReduce.
Although, probably what you really want is std::transform which can operate on two inputs and write into a third output.