Форум » C/C++ для начинающих (C/C++ for beginners) » 3D vector Row compering » Ответить

3D vector Row compering

ramees: [pre2] int v[][] = { { 1,1,1,1,2,1,2,2,2,2,3,5,5,5,5,5,57,8,8,7,8,8,8,56,56,56,56, }, { 1,1,8,1,2,1,2,2,2,2,3,5,6,6,5,5,57,8,8,7,8,8,8,56,56,56,56, }, { 1,6,8,1,1,1,2,2,3,3,3,4,4,4,5,5,57,85,8,7,8,8,8,56,56,56,1, } }; [/pre2] i have a 2d array like above i access row by row and split all same adjacent element into a 3D vector like below [pre2] std::vector<std::vector<std::vector<int>>> j = { { {1,1,1,1},{2},{1}, {2,2,2,2},{3}, {5,5,5,5,5}{57},{8,8},{7},{8,8,8},{56,56,56,56} }, { {1,1},{8},{1},{2},{1},{2,2,2,2},{3},{5},{6,6},{5,5},{57},{8,8},{7},{8,8,8},{56,56,56,56} }, { {1},{6},{8},{1,1,1},{2,2},{3,3,3},{4,4,4},{5,5},{57},{85},{8},{7},{8,8,8},{56,56,56},{1} } };[/pre2] then i convert 3D vector into ID based 3D vector like below [pre2] ID VALUE SIZE 0 1 4 std::vector<std::vector<std::vector<int>>> b = { { {0,1,4},{0,2,1},{0,1,1},{0,2,4},{0,3,1},{0,5,5},{0,57,1},{0,8,1},{0,7,1},{0,8,4},{0,56,5} }, { {0,1,2},{0,8,1},{0,1,1},{0,2,1},{0,1,1},{0,2,4},{0, 3,1},{0,5,1},{0,6,2},{0,5,2},{0,57,1},{0,8,2},{0,7,1},{0,8,3},{0,56,4} }, { {0,1,1},{0,6,1},{0,8,1},{0,1,3},{0,2,2},{0,3,3},{0,4,3},{2,5,2},{0,57,1},{0,85,1},{0,8,1},{0,7,1},{0,8,3},{0,56,3},{0,1,3} } }; [/pre2] you can see that my ID value is now zero i like to compare adjacent Row by row and give same value split to same ID i try to write this but i am stuck each split element have different size i need help

Ответов - 16

Сыроежка: This array definition [pre2] int v[][] = { { 1,1,1,1,2,1,2,2,2,2,3,5,5,5,5,5,57,8,8,7,8,8,8,56,56,56,56, }, { 1,1,8,1,2,1,2,2,2,2,3,5,6,6,5,5,57,8,8,7,8,8,8,56,56,56,56, }, { 1,6,8,1,1,1,2,2,3,3,3,4,4,4,5,5,57,85,8,7,8,8,8,56,56,56,1, } }; [/pre2] is wrong. You have to specify the number of elements that are in each row. That is you have to write [pre2] int v[][27] = { /*...*/ }; [/pre2] Also is there a typo that in the first row of the vector there is written {0,56,5} instead of {0,56,4}? And I have not understood why one element has ID number 2 : {2,5,2}. Is it also a typo?

ramees: sry my mistake this is the final 3D vector i need to compare to replace zero and Set Id (index starting from 1 to .....) [pre std::vector<std::vector<std::vector<int>>> b = { { {0,1,4},{0,2,1},{0,1,1},{0,2,4},{0,3,1},{0,5,5},{0,57,1},{0,8,2},{0,7,1},{0,8,3},{0,56,4} }, { {0,1,2},{0,8,1},{0,1,1},{0,2,1},{0,1,1},{0,2,4},{0, 3,1},{0,5,1},{0,6,2},{0,5,2},{0,57,1},{0,8,2},{0,7,1},{0,8,3},{0,56,4} }, { {0,1,1},{0,6,1},{0,8,1},{0,1,3},{0,2,2},{0,3,3},{0,4,3},{0,5,2},{0,57,1},{0,85,1},{0,8,1},{0,7,1},{0,8,3},{0,56,3},{0,1,3} } };[/pre]

Сыроежка: ramees пишет: you can see that my ID value is now zero i like to compare adjacent Row by row and give same value split to same ID I am sorry I do not understand what you mean. And what is the row now? Is it for example {0,1,4} or { {0,1,4},{0,2,1},{0,1,1},{0,2,4},{0,3,1},{0,5,5},{0,57,1},{0,8,2},{0,7,1},{0,8,3},{0,56,4} }?


ramees: [pre2] this is the row {0,1,4} { { {0,1,4},{0,2,1},{0,1,1},{0,2,4},{0,3,1},{0,5,5},{0,57,1},{0,8,2},{0,7,1},{0,8,3},{0,56,4} }, { {0,1,2},{0,8,1},{0,1,1},{0,2,1},{0,1,1},{0,2,4},{0, 3,1},{0,5,1},{0,6,2},{0,5,2},{0,57,1},{0,8,2},{0,7,1},{0,8,3},{0,56,4} } i need to compare red color row to green each row ({0,1,4}) size is different that where i stuck (the size of the row is last element ) [/pre2]

Сыроежка: I do not understand. If this {0,1,4} is a row then all rows have the same size that is equal to 3. So it is unclear what you are trying to do.

ramees: {0,1,4} is equal to 1111 so if compare 2 row like {0,1,4} and {0,1,2} it is like [pre2] 1111 , 2 , 1 , 2222 11 , 8 , 1 , 2 this is the row i used for above example { { {0,1,4},{0,2,1},{0,1,1},{0,2,4},{0,3,1},{0,5,5},{0,57,1},{0,8,2},{0,7,1},{0,8,3},{0,56,4} }, { {0,1,2},{0,8,1},{0,1,1},{0,2,1},{0,1,1},{0,2,4},{0, 3,1},{0,5,1},{0,6,2},{0,5,2},{0,57,1},{0,8,2},{0,7,1},{0,8,3},{0,56,4} } [/pre2]

Сыроежка: I do not understand these records 1111 , 2 , 1 , 2222 11 , 8 , 1 , 2 It is clear that row {0,1,4} contains four 1. But what does 2, 1, 2222 mean? How did you get them?

ramees: [pre2] that is 1111 = {0,1,4} 2 = {0,2,1} 1 = {0,1,1} , 2222 = {0,2,4} this i showed from this line = { {0,1,4},{0,2,1},{0,1,1},{0,2,4},{0,3,1},{0,5,5},{0,57,1},{0,8,2},{0,7,1},{0,8,3},{0,56,4} }, next 11 , 8 , 1 , 2 is from second row of vector { {0,1,2},{0,8,1},{0,1,1},{0,2,1},{0,1,1},{0,2,4},{0, 3,1},{0,5,1},{0,6,2},{0,5,2},{0,57,1},{0,8,2},{0,7,1},{0,8,3},{0,56,4} } [/pre2]

Сыроежка: And what you are trying to do? Till now I can not understand that. So you have two arrays (or vectors) of triads and what you want to do with them?

ramees: for example this is the my vector i initialize like this because i am using c++ 98 compiler [pre2] std::vector<std::vector<std::vector<int>>> v; int ary [3][5][3]= {{{0,5,3},{0,4,12},{0,7,1}}, {{0,5,3},{0,4,2 },{0,7,2},{0,4,8},{0,7,2}}, {{0,5,3},{0,4,5 },{0,6,1},{0,4,3},{0,7,5}}}; /* 5 5 5 5 4 4 4 4 4 4 4 4 4 4 4 4 7 5 5 5 4 4 7 7 4 4 4 4 4 4 4 4 7 7 5 5 5 4 4 4 4 4 6 4 4 4 7 7 7 7 7 */ std::vector<std::vector<int>> J; std::vector<int> B; for(int i = 0; i < 3; i++){ v.push_back(J); for(int h = 0; h < 5; h++){ v.push_back(B); for(int j = 0; j < 3; j++){ v[h].push_back(ary[h][j]); } } } [/pre2] i like to get out put like this [pre2] {{{1,5,3},{2,4,12},{5,7,1}}, {{1,5,3},{2,4,2 },{3,7,2},{2,4,8},{5,7,2}}, {{1,5,3},{2,4,5 },{4,6,1},{2,4,3},{5,7,5}}}; [/pre2] the calculation i wand is down to top the last row of 3D vector v . {{0,5,3},{0,4,5},{0,6,1},{0,4,3},{0,7,5}}}; and its adjacent row {{0,5,3},{0,4,2 },{0,7,2},{0,4,8},{0,7,2}}, so you already know this vector row data is compressed version of this two row [pre2] 5 5 5 4 4 7 7 4 4 4 4 4 4 4 4 7 7 5 5 5 4 4 4 4 4 6 4 4 4 7 7 7 7 7 [/pre2] than i need to access the row of row {0,5,3} and its adjacent row of row {0,5,3} then find its data is matching in this case the value is 5 its matching so replace the zero to put a new same index to both row of row = first {1,5,3} second {1,5,3} in this case both row have same size (the last element is the actual size of row of row) so it's easy to do but the next row of row first {0,4,5 } second {0,4,2 } it's have different size. so find it's matching or not in this case it's matching so put a new index to it . we have first row of row size is 5 and second is 2 so we have 3 data balance to check from first row so we need to increment second row of row so in this case we have first row of row {2,4,5 } second is {0,7,2} find its matching or not . its not matching so replace zero with new index {3,7,2} now we increment two second row of row so both row size is 2 so we have total size 4 so its means we need to increment second row of row one more time to match first row size so the next row of row is first is = {2,4,5} second = {0,4,8} this case its matching so replace zero with first row ID (2) {2,4,5} {2,4,8} and so on .......... i think some time its need to run twice to get the correct output if the real input like this [pre2] 5 5 5 5 4 4 4 4 4 4 4 4 4 4 4 4 7 5 5 5 4 4 7 7 7 7 4 4 4 4 4 4 7 7 5 5 5 4 4 4 4 4 6 4 4 4 7 7 7 7 7 [/pre2] the idea behind this control all adjacent same element in a 2d array using a ID(index) i hope you get the idea sorry for late replay i am hard working on a film fx my dead line is near :)

Сыроежка: I have not understood why the first two triads belonging to adjucent rows were converted to {1,5,3}. What does this 1 means? Also it is not clear what is the "balance". It is all too tangled.

ramees: i convert it like this {1,5,3} because for reducing memory. this triads {1,5,3} have 3 repetition of 5 what if this 1000 or more . 1 is ID to recognizing same adjacent triads

ramees: actually what i want is split same adjacent element from this 2d array and i need control over all split to edit or replace value 0 2 4 5 4 0 2 6 8 4 0 0 0 8 4 2 2 8 8 4 2 2 8 4 4

Сыроежка: What are there the adjacent elements in this 2D array? And how are you going to split it?

ramees: for your first ! same elements of 2d array both direction (x & y) . splitting an area of 2d array to vector is risk so what i thinking is split row by row same element from x direction and add to vector like my format triads {ID , Value , size} then next row of calculation check it previous row data is connected or not if yes add previous ID to new split if its not connected add new ID

ramees: http://shot.qip.ru/00wbdV-6Kzjld74Y/



полная версия страницы