Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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++ - Fatal编程技术网

C++ 在阵列中删除和存储重复项

C++ 在阵列中删除和存储重复项,c++,C++,此程序的目标是从阵列中删除重复项 编写一个程序,从用户输入10个整数的数组,并删除重复的数组元素 以下是一些示例输出: 请输入10个整数,每个整数后按回车键: 5. 75 10 75 5. 80 10 5. 5. 50 您输入了5个唯一的数字: 57510050 这是到目前为止我的代码 #include <iostream> using namespace std; int main() { int myint[11]; int i,x,count=10; cout <&l

此程序的目标是从阵列中删除重复项

编写一个程序,从用户输入10个整数的数组,并删除重复的数组元素

以下是一些示例输出: 请输入10个整数,每个整数后按回车键: 5. 75 10 75 5. 80 10 5. 5. 50 您输入了5个唯一的数字: 57510050

这是到目前为止我的代码

#include <iostream>
using namespace std;

int main()
{
int myint[11];
int i,x,count=10;

cout << "Please input 10 integers, hitting return after each one \n";
    for(i=0;i<10;i++){
    cin>> myint[i];
    }

    for(i=0;i<=10;i++)
    {
            for(x=i+1;x<=10;x++)
            {
                    if(myint[x]==myint[i])
                    {
                            count--;
                            for(i=x;i<=count;i++)
                            { myint[i] = myint[i+1];
                            }
                    }
            }

      }
cout << endl;
cout << " You entered "<< count << " unique numbers: " <<  endl;

    for(i=0;i<count;i++){
    cout << myint[i] << " ";
    }
return 0;
}
#包括
使用名称空间std;
int main()
{
int-myint[11];
整数i,x,计数=10;
cout myint[i];
}

对于(i=0;i最内层循环重用i,它是最外层循环的变量。使用另一个字母表示

同样奇怪的是,如果要读取10个元素,为什么要有一个数组和11个对应的循环。
此外,您可以使用 同样奇怪的是,如果要读取10个元素,为什么要有一个数组和11个对应的循环。
此外,可以用“计数”代替“< P>”来限制循环,因为问题被标记为C++,还可以在代码中使用C++习语。
#include <iostream>
#include <vector>
using namespace std;

int main(int argc, const char * argv[])
{
   vector<int> v;

   cout << "Please input 10 integers, hitting return after each one \n";
   for( int i = 0; i < 10; i++ ) {
      int num;
      cin >> num;
      v.push_back(num);
   }

   sort( v.begin(), v.end() );
   v.erase( unique( v.begin(), v.end() ), v.end() );

   cout << endl << " You entered " << v.size() << " unique numbers: " <<  endl;
   copy( v.begin(), v.end(), ostream_iterator<int>( cout, " " ) );
}
#包括
#包括
使用名称空间std;
int main(int argc,const char*argv[]
{
向量v;
cout>num;
v、 推回(num);
}
排序(v.begin(),v.end());
v、 擦除(唯一(v.begin()、v.end()、v.end());

CUT< P>由于问题被标记为C++,你还可以在代码中使用C++习语。让<代码>排序< <代码> >代码>唯一< /C> >重启。

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

int main(int argc, const char * argv[])
{
   vector<int> v;

   cout << "Please input 10 integers, hitting return after each one \n";
   for( int i = 0; i < 10; i++ ) {
      int num;
      cin >> num;
      v.push_back(num);
   }

   sort( v.begin(), v.end() );
   v.erase( unique( v.begin(), v.end() ), v.end() );

   cout << endl << " You entered " << v.size() << " unique numbers: " <<  endl;
   copy( v.begin(), v.end(), ostream_iterator<int>( cout, " " ) );
}
#包括
#包括
使用名称空间std;
int main(int argc,const char*argv[]
{
向量v;
cout>num;
v、 推回(num);
}
排序(v.begin(),v.end());
v、 擦除(唯一(v.begin()、v.end()、v.end());

cout这是@chr的解决方案中提到的集合的解决方案:

#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>

using namespace std;

int main(int argc, const char* argv[])
{
   set<int> s;

   cout << "Please input 10 integers, hitting return after each one \n";
   for( int i = 0; i < 10; i++ ) {
      int num;
      cin >> num;
      s.insert(num);
   }
   cout << endl << " You entered " << s.size() << " unique numbers: " <<  endl;
   copy( s.begin(), s.end(), ostream_iterator<int>( cout, " " ) );
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,const char*argv[]
{
设置s;
cout>num;
s、 插入(num);
}

cout这是@chr的解决方案中提到的集合的解决方案:

#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>

using namespace std;

int main(int argc, const char* argv[])
{
   set<int> s;

   cout << "Please input 10 integers, hitting return after each one \n";
   for( int i = 0; i < 10; i++ ) {
      int num;
      cin >> num;
      s.insert(num);
   }
   cout << endl << " You entered " << s.size() << " unique numbers: " <<  endl;
   copy( s.begin(), s.end(), ostream_iterator<int>( cout, " " ) );
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,const char*argv[]
{
设置s;
cout>num;
s、 插入(num);
}

您应该能够通过在调试器中逐行执行并观察变量发生了什么来调试它。您应该能够通过在调试器中逐行执行并观察变量发生了什么来调试它。甚至更好的解决方案可以是使用std:set。从本质上讲,这会处理重复项.根据开场白,排序不是一个标准。
排序
需要使
唯一
适用于这种情况。
设置
需要更多的代码和内存(保存元素副本),尽管它可以更有效地处理大量数据,正如在更好的解决方案中很好地讨论的那样,可以使用std:set。这将从本质上考虑重复项。根据开场白,排序不是一个标准。
排序
需要使
唯一
适用于这种情况。
set
需要更多的代码nd更多的内存(用于保存元素的副本),尽管对于大量数据来说,它可能更有效,如中所述