Coming from Java, I'm used to built in data structure that I can quickly/easily reference that allow me to do things like add integers to a list and check to see if an integer is in the list.
Since this seems to be common, I'm hoping to see what the easy way of doing this is C++ Builder is. I'm okay with using a standard C++ library too, as long as it will work with C++ Builder.
You could use std::vector
or std::set
. Use vector
if you want to allow duplicates, use set if you don't. Searching a vector
will also be significantly slower in comparison to a set
.
std::vector<int> intCheckList;
std::set<int> intCheckSet;
mattingly890's suggestion worked for me, this is just a follow up to give an example of how I used the set, as a reference for others. Below is some sample code on how I inserted into a set and tested to see if the value was in the set. Be sure to have #include <set>
at the top.
// Test std set
std::set<int> myset;
std::set<int>::iterator it;
myset.insert(10);
myset.insert(20);
myset.insert(30);
it = myset.find(10);
boolean found = it!=myset.end();
if(found)
MessageDlg("Found 10", mtInformation, TMsgDlgButtons() << mbOK, 0);
it = myset.find(15);
found = it!=myset.end();
if(found)
MessageDlg("Found 15", mtInformation, TMsgDlgButtons() << mbOK, 0);
it = myset.find(20);
found = it!=myset.end();
if(found)
MessageDlg("Found 20", mtInformation, TMsgDlgButtons() << mbOK, 0);
References:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.