计算数组中唯一字符串的数量

时间:2015-01-11 04:09:40

标签: c++ arrays unique

我一直在尝试制作一个简单的程序来计算数组中唯一字符串的数量,但是我仍然无法想到如果字符串重复两次以上我应该怎么做的解决方案。

这是代码的一个例子,其中" Tommy"在数组中存在3次。因此,当我计算不唯一的数量时,它应该是3,并且只有2个名称是唯一的。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string stringArray[5] = { "Tommy", "Sammy", "Tommy", "Wally", "Tommy" },     repeatedArray[5];
    int instance = 0, notUnique = 0, repeated = 0;

    for (int i = 0; i < 5; i++)
    {
        for (int j = i + 1; j < 5; j++)
        {
            if (stringArray[i] == stringArray[j] && stringArray[i] != repeatedArray[repeated])
            {
                instance++;
            }
        }

        if (instance == 1)
        {
            notUnique += 2;
        }

        else if (instance >= 2)
        {
            notUnique += instance + 1;
            repeatedArray[repeated] = stringArray[i];
            repeated++;
        }

        instance = 0;
    }

    cout << "Number of non-unique strings in array is :" << notUnique << endl;
}

2 个答案:

答案 0 :(得分:0)

编辑:我必须阅读两次以了解您想要计算的内容。值得庆幸的是,我想到的方法适用于此,所以:

一种简单的方法是记住您以易于访问的方式找到字符串的频率。我在考虑std::map

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
  string stringArray[5] = { "Tommy", "Sammy", "Tommy", "Wally", "Tommy" };

  std::map<string, int> count_find;
  int non_unique_count = 0, unique_count = 0;

  for(auto &s : stringArray) {
    // Note: This uses that std::map::operator[] zero-initializes when it is
    //       called for a new key, so crucially, we start with 0 (not uninitialized)
    int count = ++count_find[s];
    if(count == 1) {         // when a string is found the first time
      ++unique_count;        // tentatively mark it as unique
    } else if(count == 2) {  // when it is found the second time
      --unique_count;        // unmark as unique
      non_unique_count += 2; // and chalk both found strings up as non-unique
    } else {                 // after that, 
      ++non_unique_count;    // just chalk up as non-unique.
    }
  }

  std::cout << unique_count << ", " << non_unique_count << std::endl;
}

或者,您可以在循环结束后从non_unique_count计算unique_count,如

for(auto &s : stringArray) {
  int count = ++count_find[s];
  if(count == 1) {         // when a string is found the first time
    ++unique_count;        // tentatively mark it as unique
  } else if(count == 2) {  // when it is found the second time
    --unique_count;        // unmark as unique
  }
}

int non_unique_count = 5 - unique_count;

答案 1 :(得分:0)

您想要计算数组中唯一/非唯一字符串的数量,因此您只需要知道一个字符串是否唯一。

#include <iostream> #include <string> using namespace std;
 int main() {
  string stringArray[5]= {
    "Tommy", "Sammy", "Tommy", "Wally", "Tommy"
  }
  ;
  bool repeate[5];//mark whether the string is unique or not
  for(size_t i=0;
  i<5;
  ++i) repeate[i]=false;
  for (int i=0;
  i < 5;
  i++) {
    for (int j=i + 1;
    j < 5;
    j++) {
      if (stringArray[i]==stringArray[j]) {
        repeate[i]=true;
        repeate[j]=true;
      }
    }
  }
  int notUnique=0;
  for(int i=0;
  i<5;
  ++i) if(repeate[i]) ++notUnique;
  cout <<"Number of non-unique strings in array is :" << notUnique << endl;
}