15,359,134 members
See more:
I have a grid in which every cell build up of 3 nodes. I need to find the neighboring cell of every single cell in the grid. The point is if two cells share the same edge they are adjacent. Based on this strategy I wrote the following code and it's working fine but as you can see it's not an efficient code as in one point i have 4 loops with one if condition. I know this can be done with using something like mapping and set but I can't figure out how. I would be grateful if you could help me with this.

What I have tried:

```#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

typedef std::vector<int>         VecInt_t;
typedef std::vector<VecInt_t>    VecVecInt_t;

int concat(int a, int b);

int main()
{
VecVecInt_t Connectivity{ {0,1,3},{1,4,3},{1,5,4},{1,2,5},{3,7,6},{3,4,7},{4,5,7},{5,8,7} };
}

int concat(int a, int b)
{
std::string s1 = std::to_string(a);
std::string s2 = std::to_string(b);

std::string s = s1 + s2;
int c = stoi(s);

return c;
}

void VecVec_no_duplicates_unique(VecVecInt_t& Vec)
{
for (size_t i = 0; i < Vec.size(); i++) {
std::sort(Vec[i].begin(), Vec[i].end());
Vec[i].erase(std::unique(Vec[i].begin(), Vec[i].end()), Vec[i].end());
}
}

void elm_adj(VecVecInt_t& connect) {

VecVecInt_t arr(connect.size());

for (int i = 0; i < connect.size(); i++) {

int a = concat(connect[i][0], connect[i][1]);
int b = concat(connect[i][1], connect[i][0]);
int c = concat(connect[i][1], connect[i][2]);
int d = concat(connect[i][2], connect[i][1]);
int e = concat(connect[i][0], connect[i][2]);
int f = concat(connect[i][2], connect[i][0]);
arr[i].push_back(a);
arr[i].push_back(b);
arr[i].push_back(c);
arr[i].push_back(d);
arr[i].push_back(e);
arr[i].push_back(f);
}

//for (int i = 0; i < arr.size(); i++) {
//	for (int j = 0; j < arr[i].size(); j++) { // print all string in first vector of 'arr'
//		std::cout << arr[i][j] << " ";
//	}
//	std::cout << std::endl;
//}

int row = 0;
int col = 0;

for (int i = 0; i < arr.size(); i++) {
for (row = 1; row < connect.size(); row++) {
for (int j = 0; j < 6; j++) {

for (col = 0; col < 6; col++) {

if (arr[i][j] == arr[row][col]) {
}
}

}
}
}

for (int i = 0; i < Elm_Adj.size(); i++) {
for (int j = 0; j < Elm_Adj[i].size(); j++) {

std::cout << Elm_Adj[i][j] << " ";
}
std::cout << std::endl;
}
}```
Posted
Updated 27-Mar-22 22:25pm

## Solution 1

The concat with the string operation isnt good style and also slow. What about using some class for the resulting point? Maybe with some functions.
Maybe you can use some library with mathematical operations like Compositional Numeric Library. Some advantages of libraries are that it is proven and tested code in which a lot of error and edge case are already gracefully handled.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 80 CPallini 50 Dave Kreskowiak 50 dan!sh 35 ludosoep 25
 OriginalGriff 590 CPallini 310 Richard MacCutchan 240 Chris Copeland 190 0x01AA 140

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900