Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计数共用排序向量串C++_C++_String_Algorithm_Sorting_Vector - Fatal编程技术网

计数共用排序向量串C++

计数共用排序向量串C++,c++,string,algorithm,sorting,vector,C++,String,Algorithm,Sorting,Vector,我有一个字符串的排序向量,我试图找到向量中每个元素的坐标: V={AAA,AAA,AAA,BCA,…} int main() { vector<string> vec; //for every word in the vector for(size_t i = 0; i < vec.size();i++) { int counter = 0; //loop through

我有一个字符串的排序向量,我试图找到向量中每个元素的坐标:

V={AAA,AAA,AAA,BCA,…}

int main()
{
      vector<string> vec;
      //for every word in the vector
      for(size_t i = 0; i < vec.size();i++)
       {

             int counter = 0;
              //loop through the vector and count the coocurrence of this word
             for(size_t j = 0; j < vec.size();j++)
              {
                 if(vec[i] == vec[j]) counter +=1;
              }

              cout << vec[i] << "    "<<counter <<ed,l
         }
}
复杂性在^2上,对吗?这花了这么多时间,我怎么才能找到解决方法呢

谢谢,

这就是编辑:

int main()
{
      vector<string> vec;
      //for every word in the vector
      for(size_t i = 0; i < vec.size();i++)
       {

             int counter = 0;
              //loop through the vector and count the coocurrence of this word
             for(size_t j = i+1; j < vec.size()-1;j++)
              {
                 if(vec[i] == vec[j]) counter +=1;
              }

              cout << vec[i] << "    "<<counter <<ed,l
         }
}

没有测试。我假设向量至少包含一个元素

counter = 1
for(size_t i = 1; i < vec.size(); i++)
  {
    if(vec[i] == vec[i-1]) counter +=1;
    else 
      {
         std::cout << vec[i-1] << ", " << counter << std::endl;
         counter = 1;
      }
  }
std::cout << vec[i-1] << ", " << counter << std::endl;

这显然是在进行中。与您的代码有一点不同:每个单词只打印一次。

测试,打开,即使向量未排序或为空:

#include <iostream>
#include <vector>
#include <unordered_map>

int main()
{
    std::vector<std::string> v = { "aaa", "abc", "aaa", "def", "aaa", "aaa", "abc", "ghi" };
    std::unordered_map<std::string, int> m;

    for (std::vector<std::string>::iterator it = v.begin(); it != v.end(); it++)
        m[*it]++;

    for (std::unordered_map<std::string, int>::iterator it = m.begin(); it != m.end(); it++)
        std::cout << it->first << " -> " << it->second << std::endl;

    return 0;
}
或者,为了可读性,使用基于范围的循环重新编写适当的代码段,感谢Frerich Raabe:

for (const auto it: v)
    m[it]++;

for (const auto it: m)
    std::cout << it.first << " -> " << it.second << std::endl;

它已经被排序了吗?@hivert是的,向量已经被排序了。如果它没有被排序,那么把它的运行时间减半就很简单了。forsize_t j=i+1@MooingDuck:但是它仍然是开着的*nyes它将是开着的^2,所以每个单词都是这个,只要它是一样的,我就在计数器上加+1,直到它不一样。。哦,你在关于零元素的案例中编辑了一条评论:P@Mooing鸭子阅读我在这篇文章开头的假设,它确实改变了。我原以为明天中午能完成计数。我知道这不是测量时间的正确方法,只是一个近似值:Run1:5.081,Run2:6.053,Run3:5。068@hivert:这至少在*| S |上,大致在^2上。比较两个字符串需要O | S |运行时,其中| S |=字符串显示的长度,它在内存中,而我的解决方案是O1。@hivert然而,要求的是更严格的运行时间限制,而不是更低的内存使用率。如果使用std::unordered |映射和初始值设定项列表,您也可以使用基于范围的for循环和auto来大幅缩短代码,例如,对于常数自动&s:vm[s]+@H2CO3请不要太酸;-我敢打赌我的代码比你的快…我会尝试这两种方法,非常有趣。我有一个106207大小的向量。多谢各位