#include int main() { bool tri; int tab[5][4]; for (int i=0; i < 5; i++) for (int j=0; j < 4; j++) tab[i][j] = (i*j)%10; // Pour remplir le tableau un peu au hasard... tab[3][2] = -1; // met -1 ligne 3 colonne 2 std::cout << "\n"; for (int i=0; i < 5; i++) { for (int j=0; j < 4; j++) std::cout << tab[i][j] << " "; std::cout << "\n"; } std::cout << "\nAvec pointeurs : " << *(*(tab + 3) + 2); // *(*(tab + ligne) + colonne) std::cout << "\nAvec operateurs [] : " << tab[3][2]; // cette ligne comme la précédente affiche -1. for (int i=0; i < 5; i++) // pour le tri { tri=true; while (tri) { tri=false; for (int j=0; j < 4 - 1; j++) { if (tab[i][j] > tab[i][j+1]) { tri=true; int temp = tab[i][j]; tab[i][j] = tab[i][j + 1]; tab[i][j + 1] = temp; } } } } std::cout << "\n\n"; for (int i=0; i < 5; i++) // réaffiche le tableau { for (int j=0; j < 4; j++) std::cout << tab[i][j] << " "; std::cout << "\n"; } return 0; }
#include #include #include <vector> struct MaStruct { MaStruct(int a, int b, int c) : _a(a), _b(b), _c(c) { } int _a; int _b; int _c; }; void display(const std::vector<MaStruct>& tab) { for (std::vector<MaStruct>::const_iterator it = tab.begin(); it != tab.end(); ++it) std::cout << it->_a << " " << it->_b << " " << it->_c << std::endl; } bool sortFunction(const MaStruct& obj1, const MaStruct& obj2) { return obj1._c < obj2._c; } int main() { std::vector<MaStruct> tab; tab.push_back(MaStruct(3, 3, 3)); tab.push_back(MaStruct(2, 4, 4)); tab.push_back(MaStruct(8, 7, 5)); tab.push_back(MaStruct(6, 4, 3)); tab.push_back(MaStruct(2, 1, 2)); tab.push_back(MaStruct(1, 2, 1)); std::cout << "Before:" << std::endl; display(tab); std::sort(tab.begin(), tab.end(), sortFunction); std::cout << "After:" << std::endl; display(tab); return 0; }
Mettons qu'on veut accéder à la valeur tab[4][2], comment la récupérer via les pointeurs? (tab+4+m+2? tab+4+2?...)
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionvoid triDuSujet(int m, int[][3]){ int temp1; int temp2; int temp3; bool tri = true; while (tri){ tri=false; for (int i=0; i < m-1; i++){ if (edge[i][2] > edge[i+1][2]){ tri=true; temp1 = edge[i][0]; temp2 = edge[i][1]; temp3 = edge[i][2]; edge[i][0] = edge[i+1][0]; edge[i][1] = edge[i+1][1]; edge[i][2] = edge[i+1][2]; edge[i+1][0] = temp1; edge[i+1][1] = temp2; edge[i+1][2] = temp3; } } } }