OK, please do not vote immediately. I know this is a bad example. I wrote that it was the OP that specifically requested the no-STL solution, and imagine how (bad) it would look.
Well that's all. The code is not complete. But you should get a general idea. One thing I missed is sorting integers. Since it should be trivial. As you can see, the mapping is a little PIA and looks pretty bad. But since you forbid using STL, there is no std::map . Moreover, I meant the static size N for all tables. It could be distributed dynamically, without problems, and not std::vector .
I used else if for map* functions to map* functionality of std::map . You can probably use switch ... case , but it should work similarly on a decent compiler.
The code I wrote below is practically no different from the functionality provided to Armen. I would recommend his solution. I missed the same parts. So you can see it more ugly and more typical. It almost looks like a pure C. Perhaps with one modification, if you really dream of speed in very large cases. This will use a temporary data structure that will contain the displayed values, sort it and then display it back. That is why I would advise avoiding calling map::operator[](const &T) (or any accessor) on std::string in accordance with high performance limitations in order to avoid hash calculations. But that is just it.
There are a few more discussions. For example, if you want two colors to have the same value or use non-integer weights. The STL-based solution is more flexible.
Greetings.
Code:
enum Colors { Red, Orange, Green, Violet }; void mapString2Color( const std::string* input, int* output, size_t N ){ for(size_t i = 0; i < N; i++){ if ( input[i] == std::string("red") ) output[i] = Colors::Red; else if ( input[i] == std::string("orange") ) { output[i] = Colors::Orange; } else if ( input[i] == std::string("green") ) { output[i] = Colors::Green; } else if ( input[i] == std::string("violet") ) { output[i] = Colors::Violet; } else {} } } void mapColor2String( const int* input, std::string* output, size_t N ){ for(size_t i = 0; i < N; i++){ if ( input[i] == Colors::Red ) output[i] = std::string("red"); else if ( input[i] == Colors::Orange ) { output[i] = std::string("orange"); } else if ( input[i] == Colors::Green ) { output[i] = std::string("green"); } else if ( input[i] == Colors::Violet ) { output[i] = std::string("violet"); } else {} } } void sort(int* array, size_t N){ } main(){ std::string[N] input_array; std::string[N] output_array; int[N] temp_array;
luk32 source share