推送仅向量矢量一个元素,不重复

时间:2022-08-01 03:37:13

I have a code where i should introduce 3 numbers and an multi-dimensional array. I should print all numbers from array that are divisors with 3 numbers from start..

我有一个代码,我应该介绍3个数字和一个多维数组。我应该从数组中打印所有数字,这些数字是从开始的3个数字的除数。

Here's my code:

这是我的代码:

#include <vector>
#include <iostream>

using namespace std;

int main() {
    int r, p, k, nr, n, m, counter=0, temp;
    vector <int> numbers;

    cout << "Enter value of r, p, k: ";
    cin >> r >> p >> k;
    cout << "Enter the number of rows and columns: ";
    cin >> n >> m;

    int T[n][m];

    cout << "Enter values: ";
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> T[i][j];
        }
    }

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            for(int a = 0; a < 1; a++) {
                numbers.push_back(T[i][j]);
                counter++;

            }
        }
    }

    for(int f = 0; f < counter; f++) {
        if(r%numbers[f]==0 && p%numbers[f]==0 && k%numbers[f]==0) {
            cout << numbers[f] << ' ';
        }
    }

    return 0;
}

So, my question is.. how to push in vector numbers that repeats only 1 time.. I mean if in array are 2 the same number, dont print both of them but just one of them.

所以,我的问题是..如何推送只重复一次的矢量数字。我的意思是如果在数组中是2相同的数字,不要打印它们两个但只是其中之一。

Thanks in advance.

提前致谢。

2 个解决方案

#1


0  

Use a set: http://en.cppreference.com/w/cpp/container/set

使用一套:http://en.cppreference.com/w/cpp/container/set

A set does not allow duplicates. For example, if you insert the number 5 more than once, there will still only be one 5 in the set.

一套不允许重复。例如,如果您多次插入数字5,则集合中仍然只有一个5。

First #include<set>.

Then replace vector <int> numbers; with set<int> numbers;

然后替换vector 数字;使用set 数字;

Then replace

for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
        for(int a = 0; a < 1; a++) {
            numbers.push_back(T[i][j]);
            counter++;

        }
    }
}

with

for(int i = 0; i < n; i++)
    for(int j = 0; j < m; j++)
        numbers.insert(T[i][j]);

Then replace

for(int f = 0; f < counter; f++) {
     if(r%numbers[f]==0 && p%numbers[f]==0 && k%numbers[f]==0) {
         cout << numbers[f] << ' ';
     }
} 

with

for (auto i = numbers.cbegin(); i != numbers.cend(); ++i)
    if(r % *i == 0 && p % *i == 0 && k % *i == 0)
        cout << *i << ' ';

That should do it. You can eliminate the counter variable from the program because numbers.size() gives you the number of objects in the set. Also, your temp variable is not used, so eliminate that as well. Also, note that set is an ordered container, so printing it like this will print the numbers in ascending order.

应该这样做。您可以从程序中删除计数器变量,因为numbers.size()会为您提供集合中的对象数。此外,您的临时变量未使用,因此也请将其删除。另请注意,set是一个有序容器,因此像这样打印将按升序打印数字。

(Also note that the length of an array such as int arr[3]; must be known at compile time to be strictly valid C++. Here 3 is a literal and so is known at compile time. Asking the user to input the length of the array means that it is not known at compile time.)

(另请注意,数组的长度,如int arr [3];必须在编译时知道是严格有效的C ++。这里3是文字,因此在编译时已知。要求用户输入长度数组意味着它在编译时是未知的。)

#2


0  

After you fill your vector, you can first sort all elements in it and than call std::unique, to remove all duplicates from it.

填充向量后,您可以先对其中的所有元素进行排序,然后调用std :: unique,从中删除所有重复项。

Try to look references for std::unique and std::sort

尝试查看std :: unique和std :: sort的引用

#1


0  

Use a set: http://en.cppreference.com/w/cpp/container/set

使用一套:http://en.cppreference.com/w/cpp/container/set

A set does not allow duplicates. For example, if you insert the number 5 more than once, there will still only be one 5 in the set.

一套不允许重复。例如,如果您多次插入数字5,则集合中仍然只有一个5。

First #include<set>.

Then replace vector <int> numbers; with set<int> numbers;

然后替换vector 数字;使用set 数字;

Then replace

for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
        for(int a = 0; a < 1; a++) {
            numbers.push_back(T[i][j]);
            counter++;

        }
    }
}

with

for(int i = 0; i < n; i++)
    for(int j = 0; j < m; j++)
        numbers.insert(T[i][j]);

Then replace

for(int f = 0; f < counter; f++) {
     if(r%numbers[f]==0 && p%numbers[f]==0 && k%numbers[f]==0) {
         cout << numbers[f] << ' ';
     }
} 

with

for (auto i = numbers.cbegin(); i != numbers.cend(); ++i)
    if(r % *i == 0 && p % *i == 0 && k % *i == 0)
        cout << *i << ' ';

That should do it. You can eliminate the counter variable from the program because numbers.size() gives you the number of objects in the set. Also, your temp variable is not used, so eliminate that as well. Also, note that set is an ordered container, so printing it like this will print the numbers in ascending order.

应该这样做。您可以从程序中删除计数器变量,因为numbers.size()会为您提供集合中的对象数。此外,您的临时变量未使用,因此也请将其删除。另请注意,set是一个有序容器,因此像这样打印将按升序打印数字。

(Also note that the length of an array such as int arr[3]; must be known at compile time to be strictly valid C++. Here 3 is a literal and so is known at compile time. Asking the user to input the length of the array means that it is not known at compile time.)

(另请注意,数组的长度,如int arr [3];必须在编译时知道是严格有效的C ++。这里3是文字,因此在编译时已知。要求用户输入长度数组意味着它在编译时是未知的。)

#2


0  

After you fill your vector, you can first sort all elements in it and than call std::unique, to remove all duplicates from it.

填充向量后,您可以先对其中的所有元素进行排序,然后调用std :: unique,从中删除所有重复项。

Try to look references for std::unique and std::sort

尝试查看std :: unique和std :: sort的引用