How to make an array without the use of square brackets?
Closed due to the fact that the essence of the issue is not clear to the participants of Kromster , Cerbo , LFC , AnT , 0xdb on March 15 at 8:48 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- Unclear. Give examples, describe in more detail what you want. - Mikhail Murugov
- And you can reformulate the question? An array is just a convenient way to access ordered data. - MBo
- Macro via #define - nick_n_a
- Use the pointer - avp pm
|
2 answers
Like a bicycle without wheels ... Do you explain exactly what you want?
Brackets cannot be used categorically? At all? then it is necessary through malloc
.
int a[10]; a[1] = 5;
change to
int*a = (int*)malloc(sizeof(int)*10); *(a+1) = 5;
It'll do?
- 3You can trigraph more.
a??(10??)
:) - Alexey Ten
|
#include <string> #include <iterator> #include <iostream> #include <algorithm> #include <array> int main() { // ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ Π°Π³ΡΠ΅Π³Π°ΡΠ½ΡΠΉ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΎΡ std::array<int, 3> a1{ {1,2,3} }; // ΡΡΠ΅Π±ΡΡΡΡΡ Π΄Π²ΠΎΠΉΠ½ΡΠ΅ ΡΠΈΠ³ΡΡΠ½ΡΠ΅ ΡΠΊΠΎΠ±ΠΊΠΈ, std::array<int, 3> a2 = {1, 2, 3}; // Π·Π° ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ΠΌ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ ΠΏΡΠΈΡΠ²Π°ΠΈΠ²Π°Π½ΠΈΡ std::array<std::string, 2> a3 = { {std::string("a"), "b"} }; // ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°ΡΡΡΡ ΠΎΠ±ΠΎΠ±ΡΡΠ½Π½ΡΠ΅ Π°Π»Π³ΠΎΡΠΈΡΠΌΡ std::sort(a1.begin(), a1.end()); std::reverse_copy(a2.begin(), a2.end(), std::ostream_iterator<int>(std::cout, " ")); // ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅ΡΡΡ ranged for ΡΠΈΠΊΠ» for(auto& s: a3) std::cout << s << ' '; }
|