Gold mine of Visual C++ tricks!
Seek uniqueness? Use STL set.
![]()
In several conditions we’ve to worry about avoiding duplicate data. For instance, if you have a combo-box which contains browsed file paths as history, it will be nice to avoid multiple occurrence of same file path. In those cases you can make use of STL set to remove duplicate data.
![]()
Set is a template class like any other STL container. For adding new data to set, you’ve to call insert() and for iterating you can use iterators. See sample code snippet below.
#include "set"
#include "string"
using namespace std;
...
// Set which is used to hold FilePaths as strings.
set<string> FilePathSet;
// I am inserting the same path a number of times.
// But finally, while iterating you can see only one
// copy of the same data.
FilePathSet.insert("C:\Windows\System32");
FilePathSet.insert("C:\Windows\System32");
FilePathSet.insert("C:\Windows\System32");
FilePathSet.insert("C:\Windows\System32");
// Iterate through the set.
set<string>::iterator FilePathSetItr = FilePathSet.begin();
while( FilePathSetItr != FilePathSet.end())
{
// The unique data. Use it.
string UniqueFilePath = *FilePathSetItr;
// Increment to next item in set.
++FilePathSetItr;
}
![]()
Targeted Audience – Beginners.
| Print article | This entry was posted by Jijo Raj on May 8, 2008 at 4:00 pm, and is filed under C++, Codeproject. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |










about 1 year ago
wkdomwwncrnuxxknwell, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch
about 1 year ago
Hello Attameceam,
Welcome to WeSeeTips. So happy to know that you like it. Well, keep watching and enjoy!
Regards,
Jijo.