英文:
How do I sort the words in a vector in alphabetical order?, (all letters in each word need to be accounted for)
问题
I'm trying to make a function that sorts the words in a vector and puts them in alphabetical order, and if the letters are all the same letters the word with fewest letters ends up higher in the vector.
Right now everything seems to work except when words with all the same letters end up in a random order.
For example, the words a
, aaa
, and aaaa
would end up in a random order, but I would want a
to be at the top of the list and aaaa
at the bottom.
void sortWords()
{
for (int i = 0; i < wordList.size(); i++)
{
for (int j = 0; j < wordList.size(); j++)
{
string temp;
for (int y = 0; y < wordList[i].size(); y++)
{
if (wordList[i][y] < wordList[j][y] && i != j)
{
temp = wordList[i];
wordList[i] = wordList[j];
wordList[j] = temp;
break;
}
else if (wordList[i][y] > wordList[j][y] && i != j)
{
break;
}
}
}
}
}
英文:
I'm trying to make a function that sorts the words in a vector and puts them in alphabetical order, and if the letters are all the same letters the word with fewest letters ends up higher in the vector.
Right now everything seems to work except when words with all the same letters end up in a random order.
For examlpe the words a
, aaa
and aaaa
would end up in a random order, but I would want a
to be at the top of the list and aaaa
at the bottom.
void sortWords()
{
for (int i = 0; i < wordList.size(); i++)
{
for (int j = 0; j < wordList.size(); j++)
{
string temp;
for (int y = 0; y < wordList[i].size(); y++)
{
if (wordList[i][y] < wordList[j][y] && i != j)
{
temp = wordList[i];
wordList[i] = wordList[j];
wordList[j] = temp;
break;
}
else if (wordList[i][y] > wordList[j][y] && i != j)
{
break;
}
}
}
}
}
答案1
得分: 1
如何将向量中的单词按字母顺序排序?
使用std,它将简单地是
#include <algorithm>
#include <vector>
#include <string>
void sortWords(std::vector<std::string>& words)
{
std::sort(words.begin(), words.end());
}
英文:
> How do i sort the words in a vector in alphabetical order?
With std, it would simply be
#include <algorithm>
#include <vector>
#include <string>
void sortWords(std::vector<std::string>& words)
{
std::sort(words.begin(), words.end());
}
答案2
得分: 0
You write wordList[j][y]
without checking whether y <= wordList[j].size()
first. This is a potential error: using std::string::operator[]
with an index greater than size()
causes undefined behavior. You should never, ever allow this - always check whether you're out of array bounds before accessing an array. Once you account for that, your comparison subroutine will work correctly. In a separate function:
// <0 if a<b, >0 if a>b, =0 if a and b are the same, same logic as strcmp()
int compareLexicographic(const std::string& a, const std::string& b){
//use size_t as index type
for(size_t i = 0; i < a.size(); ++i){
if(i == b.size()) return 1; //b is over, a still isn't
//now we can access a[i] and b[i]
if(a[i] < b[i]) return -1;
if(a[i] > b[i]) return 1;
}
//reached the end of a
if(a.size() == b.size()) return 0;
else return -1;
}
(As a side note, it's better to pass a pointer to wordList
as an argument of sortWords()
. Functions modifying global variables with little indication on the caller side make debugging very frustrating, you probably don't want to do this to yourself.)
英文:
You write wordList[j][y]
without checking whether y <= wordList[j].size()
first. This is a potential error: using std::string::operator[] with index greater than size() causes undefined behavior. You should never, ever allow this - always check whether you're out of array bounds before accessing an array. Once you account for that, your comparison subroutine will work correctly. In a separate function:
// <0 if a<b, >0 if a>b, =0 if a and b are the same, same logic as strcmp()
int compareLexicographic(const std::string& a, const std::string& b){
//use size_t as index type
for(size_t i = 0; i < a.size(); ++i){
if(i == b.size()) return 1; //b is over, a still isn't
//now we can access a[i] and b[i]
if(a[i] < b[i]) return -1;
if(a[i] > b[i]) return 1;
}
//reached the end of a
if(a.size() == b.size()) return 0;
else return -1;
}
(As a side note, it's better to pass a pointer to wordList
as an argument of sortWords()
. Functions modifying global variables with little indication on the caller side make debugging very frustrating, you probably don't want to do this to yourself.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论