-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagrams_sort.cpp
executable file
·63 lines (52 loc) · 1.99 KB
/
anagrams_sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
const auto LETTER_COUNT = 4;
std::vector<std::string> populate_anagram_list(){
srand(time(0));
const auto MAX_WORDS = 100;
std::vector<std::string> anagramList;
anagramList.reserve(MAX_WORDS);
for( auto i = 0; i < MAX_WORDS; ++i){
std::string strHolder(LETTER_COUNT, ' ');
std::generate(strHolder.begin(),strHolder.end(),[](){return static_cast<char>('a'+(rand()%LETTER_COUNT));});
anagramList.push_back(std::move(strHolder));
}
return anagramList;
}
// Above is the original function and below is the more highly optimized one.
// I still need to figure out how to allocate a vector of contiguous string objects (perhaps an allocator?)
// Will check the assembly code to see if RVO is done for the whole vector
/*
std::vector<std::string(6, ' ')> populate_anagram_list(){
srand(time(0));
const auto MAX_WORDS = 100;
std::vector<std::string> anagramList;
anagramList.reserve(MAX_WORDS);
for( auto i = 0; i < MAX_WORDS; ++i){
std::generate(anagramList[i].begin(),anagramList[i].end(),[](){return static_cast<char>('a'+(rand()%LETTER_COUNT));});
}
return anagramList;
}*/
int main (){
auto anagramList = populate_anagram_list();
auto print_anagram = [&anagramList](){
std::cout<< std::endl;
for(auto anagram = anagramList.begin(); anagram != anagramList.end(); ++anagram)
std::cout<< *anagram << ' ';
std::cout<< std::endl;
std::cout<< "================================" <<std::endl;
};
print_anagram();
auto sort_func = [](std::string str1, std::string str2){
std::sort(str1.begin(), str1.end());
std::sort(str2.begin(), str2.end());
return !str1.compare(str2); // Compare returns 0 when they are equal
};
std::sort(anagramList.begin(), anagramList.end(), sort_func);
print_anagram();
return 0;
}