Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.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++_Algorithm_Vector - Fatal编程技术网

C++ 找到向量中连续数的更有效方法?

C++ 找到向量中连续数的更有效方法?,c++,algorithm,vector,C++,Algorithm,Vector,我需要编写一个程序,以升序打印文件中特定单词的行号。该程序的输出示例如下: Hello 1, 3, 5-6, 8, 10-15. // 5-6 represents 5, 6 (consecutive numbers) 行号存储在排序的向量中,没有重复项。我创造了一个解决方案,但直觉告诉我我可以做得更好。我希望得到一些反馈。此外,以下代码仅处理行号。这个词可以用另一种方法找到 void IndexPager::createLines(vector<int>& vec, s

我需要编写一个程序,以升序打印文件中特定单词的行号。该程序的输出示例如下:

 Hello 1, 3, 5-6, 8, 10-15.
// 5-6 represents 5, 6 (consecutive numbers)
行号存储在排序的
向量中,没有重复项。我创造了一个解决方案,但直觉告诉我我可以做得更好。我希望得到一些反馈。此外,以下代码仅处理行号。这个词可以用另一种方法找到

void IndexPager::createLines(vector<int>& vec, string& line)
{
  int start = *vec.begin(), end = -1, offset = 0; // initial offset to start

  for (vector<int>::const_iterator itr = vec.begin();
       itr != vec.end() + 1; itr++)
  {
    if (*itr == start + offset && itr != vec.end())
    {
      end = *itr;
      ++offset;
    } // check if line numbers are consecutive and not reading at end of vector
    else // not consecutive
    {
      if ((end != -1) && (end != start))
      {
        line.append(intToString(start) + "-");
        line.append(intToString(end));
      } // if there existed consecutive numbers, display with dash
        // must be difference of at least 1
      else // else, there were no consecutive numbers
        line.append(intToString(start));

      if (itr != vec.end()) // check if not at end of vector
        line.append(", ");
      else // reached end of vector
        line.append(".");

      start = *itr; // set start to next line number. Soft reset.
      offset = 1; // change default offset to 1. 0 for first case.
      end = -1;
    } // not consecutive
  } // Get all line numbers and format for proper output
} // createLines()
void IndexPager::createLines(向量和向量、字符串和行)
{
int start=*vec.begin(),end=-1,offset=0;//起始偏移量
for(vector::const_迭代器itr=vec.begin();
itr!=vec.end()+1;itr++)
{
如果(*itr==start+offset&&itr!=vec.end())
{
结束=*itr;
++抵消;
}//检查行号是否连续且不在向量末尾读取
else//不连续
{
如果((结束!=-1)&(结束!=开始))
{
行。追加(intToString(start)+“-”);
追加(intToString(end));
}//如果存在连续数字,则用破折号显示
//差异必须至少为1
else//else,没有连续的数字
追加(intToString(start));
if(itr!=vec.end())//检查是否不在向量末尾
行。追加(“,”);
else//到达向量的末尾
第行。追加(“.”);
start=*itr;//将start设置为下一行号。软重置。
offset=1;//对于第一种情况,将默认偏移量更改为1。
结束=-1;
}//不连续
}//获取正确输出的所有行号和格式
}//createLines()

一些观察结果

  • 假设向量长度大于0
  • 根据向量实现的不同,vec.end()+1可能不会给出预期的结果 在循环体的开头增加itr,而不是在循环体的结尾使用for,可以解决这两个问题

    在效率方面,您可以尝试将“开始+偏移”替换为“下一步”

    void IndexPager::createLines(向量和向量、字符串和行)
    {
    int开始,结束,下一步;
    vector::const_迭代器itr=vec.begin();
    下一步=结束=开始=*itr;
    while(itr!=vec.end())
    {
    ++itr;
    结束=下一个++;
    如果(itr==vec.end()| | next!=*itr)
    {
    //连续中断
    追加(intToString(start));
    如果(结束!=开始){
    行。追加(“-”);
    追加(intToString(end));
    }
    如果(itr!=vec.end())
    {
    行。追加(“,”);
    }
    下一步=结束=开始=*itr;
    }
    }//获取正确输出的所有行号和格式
    第行。追加(“.”);
    }//createLines()
    

    iTR仍然被检查为vc.Enter()每次迭代三次,所以你可能想在循环结束时重复中断序列,并使用一个正常的(Auto I:VEC),但是编译器优化可以消除重复< /P>如果你想对工作代码进行一些反馈,考虑把它放在上面。但是,您必须包括一个完全可编译的解决方案;阅读帮助中心:
    void IndexPager::createLines(vector<int>& vec, string& line)
    {
        int start,end,next;
        vector<int>::const_iterator itr = vec.begin();
        next=end=start=*itr;
        while(itr!=vec.end())
        {
            ++itr;
            end=next++;
            if(itr==vec.end()||next!=*itr)
            {
                // break in consecutive sequence
                line.append(intToString(start));
                if(end!=start){
                    line.append("-");
                    line.append(intToString(end));
                }
                if(itr!=vec.end())
                {
                    line.append(", ");
                }
                next=end=start=*itr;
            }
        } // Get all line numbers and format for proper output
        line.append(".");
    } // createLines()