Forum » Programiranje » [C++] Kopiranje char arraya v drug char array
[C++] Kopiranje char arraya v drug char array

c0dehunter ::
Tare me slednja težava (poenostavljen primer):
Definirano imamo dvodimenzionalno char polje char m [2][10]; (kjer sta 2 vrstici, vsaka ima 10 znakov).
Z ukazom cin dobimo v vrstici neki besedi.
Zdaj pa bi rad recimo besedi razvrstil glede na njihovi dolžini. To bi storil takole:
Vendar c++ ne dopusti kopiranje niza v polje charov, niti polje charov v polje charov. Kako torej zamenjat (kopirat) ?
Hvala!
Definirano imamo dvodimenzionalno char polje char m [2][10]; (kjer sta 2 vrstici, vsaka ima 10 znakov).
Z ukazom cin dobimo v vrstici neki besedi.
Zdaj pa bi rad recimo besedi razvrstil glede na njihovi dolžini. To bi storil takole:
string beseda1=m[0]; string beseda2=m[1]; if(beseda2.length() > beseda1.length()){ m[0]=beseda2; m[1]=beseda1; }
Vendar c++ ne dopusti kopiranje niza v polje charov, niti polje charov v polje charov. Kako torej zamenjat (kopirat) ?
Hvala!
I do not agree with what you have to say,
but I'll defend to the death your right to say it.
but I'll defend to the death your right to say it.
- spremenilo: c0dehunter ()

snow ::
Kaj če bi že kar od začetka polnil zadevo v stringe? Lahko kar v en vector<string>.
Če hočeš zadevo za poljubno število vrstic uporabiš sort(iz <algorithm>) mu podaš neko compare funkcijo:
Če hočeš zadevo za poljubno število vrstic uporabiš sort(iz <algorithm>) mu podaš neko compare funkcijo:
bool stringCompare( const string &left, const string &right) { if(left.size() < right.size()) { return true; } return false; }
Random mutation plus nonrandom cumulative natural selection - Richard Dawkins
Zgodovina sprememb…
- spremenilo: snow ()

c0dehunter ::
Point naloge je, da se dela z dvodimenzionalnim char arrayem (tako zahteva profesor).. Poleg tega se mora naloga rešiti z čimmanj že napisanimi funkcijami in algoritmi.
P.s.: strcpy mislim da bi tu prišla v poštev, vendar mi nikakor ne rata.
P.s.: strcpy mislim da bi tu prišla v poštev, vendar mi nikakor ne rata.
I do not agree with what you have to say,
but I'll defend to the death your right to say it.
but I'll defend to the death your right to say it.

Tutankhamun ::
strcpy(&m[0], beseda1.c_str());
strcpy(&m[1], beseda2.c_str());
Probi, pa upam da nism kej ustrelu mim ko sm na hit napisu
strcpy(&m[1], beseda2.c_str());
Probi, pa upam da nism kej ustrelu mim ko sm na hit napisu
AMD Phenom QUAD 9950 Black Edition, 8GB

c0dehunter ::
Tuthankhamun, na tak način koda sicer gre čez compiler, ampak med izvajanjem programa se le-ta preneha odzivati ko pride do točke, ko bi moral urediti besede. To se mi je drugače že zgodilo, ampak e vem kaj je narobe.
I do not agree with what you have to say,
but I'll defend to the death your right to say it.
but I'll defend to the death your right to say it.

ERGY ::
Ne vem, če sem lih zadel bistvo naloge ...
#include <iostream> #include <cstring> const unsigned ST_VRSTIC = 2; const unsigned DOLZINA_NIZA = 10; void sortPolje(char polje[ST_VRSTIC][DOLZINA_NIZA]) { std::string first_str(polje[0]), second_str(polje[1]); if (first_str.length() > second_str.length() ) { std::swap(polje[0], polje[1]); } } int main() { char polje[ST_VRSTIC][DOLZINA_NIZA]; strcpy(polje[0], "123456789"); strcpy(polje[1], "12345"); sortPolje(polje); std::cout << polje[0] << "\n"; std::cout << polje[1] << "\n"; return 0; }

Tutankhamun ::
am kaj se neha odzivat :)? pri funkciji strcpy? Al kje drugje?
AMD Phenom QUAD 9950 Black Edition, 8GB

win64 ::
strcpy(&m[0], beseda1.c_str()); strcpy(&m[1], beseda2.c_str());
Ne verjamem da bi to prišlo skozi prevajalnik.
Če je m tipa char[2][10], torej je m[0] tipa char[10], kar pa je na nek način kazalec na polje.
Z & torej rečeš naslov od naslova do m[0], kar je v tem kontekstu nepravilno.
Torej:
strcpy(m[0], beseda1.c_str()); strcpy(m[1], beseda2.c_str());
sicer pa najbolj enostavno, če že moraš uporabljati dvodimenzionalne tabele, da le-te uporabljaš povsod in se izogibaš string.
V tvojem primeru:
char beseda1[10]=m[0]; char beseda2[10]=m[1]; if(beseda2.length() > beseda1.length()){ m[0]=beseda2; m[1]=beseda1; }
strcpy(&m[0], beseda1.c_str()); strcpy(&m[1], beseda2.c_str());
Edit: slovnica
Zgodovina sprememb…
- spremenil: win64 ()
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | križci krožci c # (strani: 1 2 )Oddelek: Programiranje | 12211 (10870) | Yacked2 |
» | [C++] vprašanja (strani: 1 2 3 4 5 6 7 8 9 )Oddelek: Programiranje | 27637 (12183) | aljazko1995 |
» | [C++] Pretvorba iz int v char *Oddelek: Programiranje | 2982 (2358) | Gundolf |
» | operator +Oddelek: Programiranje | 1299 (1172) | MasterBlaster |
» | int to string v c++Oddelek: Programiranje | 2363 (2091) | OwcA |