for (int i = 0; i < LIMIT; i++)
{
for (int j = 0; j < LIMIT; j++)
{
int value = matrix[i][j];
for (int a = 0; a < LIMIT; a++)
{
for (int b = 0; b < LIMIT; b++)
{
if (value == matrix[a][b])
count++;
if (count == 2)
{
std::cout << "Indexes of elements: " << "[" << i << ", " << j << "], ";
std::cout << "[" << a << ", " << b << "]" << std::endl;
// exit
}
}
}
count = 0;
}
}
std::vector<std::vector<int>> matrix = {{1,2,3,42},{5,6,7,8},{9,10,11,42}};
const size_t limit = matrix.size() * matrix.front().size();
const size_t cols = matrix.front().size();
size_t i = 0;
size_t j = 1;
while(i < limit - 1 && matrix[i / cols][i % cols] != matrix[j / cols][j % cols])
{
if(++j == limit) j = ++i + 1;
}
if(i < limit - 1)
{
std::cout << "Indices of elements: [" << i / cols << "," << i % cols << "]";
std::cout << "[" << j / cols << "," << j % cols << "]" << std::endl;
}
else std::cout << "Not found" << std::endl;
/* looks for some indices */
std::pair<size_t, size_t> findSomething(/* some parameters */) {
// ...
return value;
// ...
}
// ...
auto result = findSomething(/* some arguments */);
std::cout << "[" << << result.first << ", " << result.second << "]" << std::endl;
LIMIT = ...
matrix = ...
count = ...
[&]{
for (int i = 0; i < LIMIT; i++) {
for (int j = 0; j < LIMIT; j++){
int value = matrix[i][j];
for (int a = 0; a < LIMIT; a++) {
for (int b = 0; b < LIMIT; b++) {
if (value == matrix[a][b]){
count++;
}
if (count == 2) {
std::cout << "Indexes of elements: " << "[" << i << ", " << j << "], ";
std::cout << "[" << a << ", " << b << "]" << std::endl;
return;
}
}
}
count = 0;
}
}
}();
#define MAX_SAME 2
...
void search_matrix( int* matr, const int limit )
{
int same_count = 0;
int i = 0;
int j = i + 1;
while ( same_count < MAX_SAME && i < limit*limit - 1 ) {
if ( matr[ i ] == matr[ j ] ) {
same_count++;
}
if ( same_count < MAX_SAME ) {
j++;
if ( j == limit*limit ) {
i++;
same_count = 0;
j = i + 1;
}
}
}
if ( same_count == MAX_SAME ) {
cout << same_count << ' ' << i << ' ' << j;
}
}