C++ 如何在c+中计算字符串数据中不同数量的字符数+;?

C++ 如何在c+中计算字符串数据中不同数量的字符数+;?,c++,C++,我可以使用length()函数计算字符串中的字符数。 但是我想计算字符串中不同字符的数量。 i、 说字符串是“你好,世界” 这里不同字符串的数目是H,e,l,o,w,r,d。 所以有8个不同的字符。最好的方法是一种称为频率检查的方法。基本上创建一个大小为128的向量。遍历字符串,并为每个字符增加与其ASCII值匹配的频率。最后,迭代freq向量并计算有多少个非零条目。代码应该如下所示: #include<iostream> #include<vector> #includ

我可以使用length()函数计算字符串中的字符数。 但是我想计算字符串中不同字符的数量。 i、 说字符串是“你好,世界” 这里不同字符串的数目是H,e,l,o,w,r,d。
所以有8个不同的字符。

最好的方法是一种称为频率检查的方法。基本上创建一个大小为128的向量。遍历字符串,并为每个字符增加与其ASCII值匹配的频率。最后,迭代freq向量并计算有多少个非零条目。代码应该如下所示:

#include<iostream>
#include<vector>
#include<string>

using namespace std;

int main()
{
string s = "Hello World";
vector<int>freq(128);

for(int i = 0; i < s.length(); i++)
    freq[s[i]]++;

int counter = 0;
for(int i = 0; i < 128; i++)
    if(freq[i] > 0)
        counter++;

cout << counter << "\n";
}

最好的方法是一种称为频率检查的方法。基本上创建一个大小为128的向量。遍历字符串,并为每个字符增加与其ASCII值匹配的频率。最后,迭代freq向量并计算有多少个非零条目。代码应该如下所示:

#include<iostream>
#include<vector>
#include<string>

using namespace std;

int main()
{
string s = "Hello World";
vector<int>freq(128);

for(int i = 0; i < s.length(); i++)
    freq[s[i]]++;

int counter = 0;
for(int i = 0; i < 128; i++)
    if(freq[i] > 0)
        counter++;

cout << counter << "\n";
}

要计算唯一字符的数量,可以使用
sd::sort
后跟
std::unique
。它将重新排列内容,并将迭代器返回到字符串中最后一个唯一的字符。减去
begin()
得到结果。

要计算唯一字符的数量,可以使用
sd::sort
后跟
std::unique
。它将重新排列内容,并将迭代器返回到字符串中最后一个唯一的字符。减去
begin()
,就得到了结果。

我认为无序映射是实现这一点的最佳方法

如果您想要字符串中的字符总数,请按唯一字符分组,下面是代码

#include <iostream>
#include <unordered_map>

using namespace std;

int main() {
    string s="test string";
    unordered_map<char,int> map;

    for (const char &c: s) { //for each char in string
        map[c]++;           
    }
    for (auto &e: map)  //for each unique char in map
            cout<<"char: "<<e.first<<" number: "<<e.second<<endl;

return 0;

    }
但是如果你只想要唯一字符的总数

#include <iostream>
#include <unordered_map>

using namespace std;
int main() {
    string s="test string";
    unordered_map<char,int> map;

    for (const char &c: s) {
        map[c]++;
    }
    int count =0;
    for (auto &e: map)
       count++;

    cout<<"Unique chars: "<<count<<endl;

}

我认为无序的_图是实现这一目标的最佳方式

如果您想要字符串中的字符总数,请按唯一字符分组,下面是代码

#include <iostream>
#include <unordered_map>

using namespace std;

int main() {
    string s="test string";
    unordered_map<char,int> map;

    for (const char &c: s) { //for each char in string
        map[c]++;           
    }
    for (auto &e: map)  //for each unique char in map
            cout<<"char: "<<e.first<<" number: "<<e.second<<endl;

return 0;

    }
但是如果你只想要唯一字符的总数

#include <iostream>
#include <unordered_map>

using namespace std;
int main() {
    string s="test string";
    unordered_map<char,int> map;

    for (const char &c: s) {
        map[c]++;
    }
    int count =0;
    for (auto &e: map)
       count++;

    cout<<"Unique chars: "<<count<<endl;

}

您可以编写自己的函数来处理这种情况

#include <iostream>

using namespace std;

string uniqueChars(string str) {
  string newStr = "";
  bool arr[128];
  for(int i = 0;i < 128; i++) {
    arr[i] = false;
  }
  char c;
  for(int i = 0, n = str.length();i < n; i++) {
    c = str[i];
    if(c < 0 || c > 127) {
      continue;
    }
    if(!arr[c]) {
      arr[c] = true;
      newStr += c;
    }
  }
  return newStr;
}

int main(void) {
  string a = "Hello It's a wonderful world";
  string b = uniqueChars(a);
  cout << a << " => " << a.length() << "\n" <<
    b << " => " << b.length();
  return 0;
}

您可以编写自己的函数来处理这种情况

#include <iostream>

using namespace std;

string uniqueChars(string str) {
  string newStr = "";
  bool arr[128];
  for(int i = 0;i < 128; i++) {
    arr[i] = false;
  }
  char c;
  for(int i = 0, n = str.length();i < n; i++) {
    c = str[i];
    if(c < 0 || c > 127) {
      continue;
    }
    if(!arr[c]) {
      arr[c] = true;
      newStr += c;
    }
  }
  return newStr;
}

int main(void) {
  string a = "Hello It's a wonderful world";
  string b = uniqueChars(a);
  cout << a << " => " << a.length() << "\n" <<
    b << " => " << b.length();
  return 0;
}


将每个字符添加到std::map中应该可以做到这一点。这将是非常浪费/低效的。如果您不关心Unicode,数组就可以了。将每个字符添加到std::map中就可以了。这将是非常浪费/低效的。如果你不关心Unicode,数组就可以了。除了这不是散列:)问题并没有指定ASCII。使用
CHAR\u MAX
而不是假设127。@500 InternalServerError它将identity函数用作哈希函数,但这不是哈希:)尽管如此,问题并没有指定ASCII。使用
CHAR\u MAX
而不是假设127。@500 InternalServerError它将标识函数用作哈希
std::unique
仅在已排序的集合中有效。因此,应该首先调用
std::sort
。更准确地说,
std::unique
只对连续的相等元素进行洗牌,因此为了达到OP之后的效果,应该首先调用
std::sort
。@john:嗯,它也适用于未排序的集合,但结果会有所不同(它仅删除相邻的重复项).
std::unique
仅在已排序的集合中起作用。因此应首先调用
std::sort
。更准确地说,
std::unique
仅对连续的相等元素进行混洗,因此为了达到OP之后的效果,应首先调用
std::sort
。@john:嗯,它在未排序的集合中起作用oo,但结果会有所不同(它只删除相邻的重复项)。这具有可怕的算法复杂性。@不幸的是,这是真的,uniqueChars在字符串上循环,每次迭代调用时都会在字符串上包含哪些循环,好吧,我已经编辑了我的帖子以使其更好,但是将
arr
初始化为某个值(零?)你真正需要的是一个
bool
数组。为什么129不是128?一个
char
可以有负值,你可能希望过滤掉或处理它以避免坏的内存访问。是的,使用bool是个好主意,因为我不需要数组中的值是精确的char,好的,我会再次编辑它;)您仍然需要初始化数组,否则您将与未知值进行比较。这具有可怕的算法复杂性。@Withwings不幸的是,这是真的,uniqueChars循环在字符串上,每次迭代调用时都包含哪些循环在字符串上,好的,我已经编辑了我的帖子,以便更好这更好,但是将
arr
初始化为某个值(零?),您真正需要的只是一个
bool
数组。为什么不是128?一个
char
可以有负值,您可能希望过滤掉或处理它,以避免坏的内存访问。是的,使用bool是个好主意,因为我不需要数组中的值是精确的char,好的,我将再次编辑它;)您仍然需要初始化阵列,否则将与未知值进行比较。这具有可怕的内存/存储复杂性。这具有可怕的内存/存储复杂性。