Sort the words in a vector in alphabetical order. 将向量中的单词按字母顺序排序。

huangapple go评论66阅读模式
英文:

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 &lt; wordList.size(); i++)
	{
		for (int j = 0; j &lt; wordList.size(); j++)
		{
			string temp;
			for (int y = 0; y &lt; wordList[i].size(); y++)
			{
				if (wordList[i][y] &lt; wordList[j][y] &amp;&amp; i != j) 
				{
					temp = wordList[i];
					wordList[i] = wordList[j];
					wordList[j] = temp;
					break;
				}
				else if (wordList[i][y] &gt; wordList[j][y] &amp;&amp; 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 &lt;algorithm&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

void sortWords(std::vector&lt;std::string&gt;&amp; 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 &lt;= 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:

// &lt;0 if a&lt;b, &gt;0 if a&gt;b, =0 if a and b are the same, same logic as strcmp()
int compareLexicographic(const std::string&amp; a, const std::string&amp; b){
  //use size_t as index type
  for(size_t i = 0; i &lt; a.size(); ++i){
    if(i == b.size()) return 1; //b is over, a still isn&#39;t
    //now we can access a[i] and b[i]
    if(a[i] &lt; b[i]) return -1;
    if(a[i] &gt; 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.)

huangapple
  • 本文由 发表于 2023年4月17日 17:04:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定