For example,
double myArray[3] = {0.0};
myArray["SSN_1"] = 2.0;
myArray["SSN_2"] = 3.0;
myArray["SSN_3"] = 2.0;
for(... how? ...)
{
... Print ... but, how? ...
}
If you have any reference, please link.
For example,
double myArray[3] = {0.0};
myArray["SSN_1"] = 2.0;
myArray["SSN_2"] = 3.0;
myArray["SSN_3"] = 2.0;
for(... how? ...)
{
... Print ... but, how? ...
}
If you have any reference, please link.
Not in c it's not possible, only integers are allowed.
Associative containers can be built with a simple struct like
struct double_map_item {
char *key;
double value;
};
take a look at bsearch()'s manual page example for a simple method to find items through their key values.
Of course, this is a very simplistic possible implementation, for a more complete and robust implementation you should read about hash tables.
Regarding this comment, the type of a character constant in c is int and so
array['x'] = value;
is valid, but note that this is not really useful because,
NOTE: following your own comment, there is a chance that you saw c++ code. In c++ you can overload the [] operator so that it takes const char * as a parameter and then use a hash table or any other method to find the matching element for the given key.
But in c, it is not possible.
If one were to implement such a container in c++, the following example illustrate how to proceed, please note that this is just for illustration of the underlying concept, you should use std::map instead,
#include <iostream>
#include <map>
class Array {
public:
Array();
double operator[](const char *const key);
void insert(const char *const key, double value);
private:
std::map<const char *, double> m_items;
};
Array::Array()
{
}
double
Array::operator[](const char *const key)
{
return m_items[key];
}
void
Array::insert(const char *const key, double value)
{
m_items.insert(std::pair<const char *, double>(key, value));
}
int
main(void)
{
Array items;
items.insert("SSN_1", 2.0);
items.insert("SSN_2", 3.0);
items.insert("SSN_3", 2.0);
std::cout << items["SSN_1"] << std::endl;
return 0;
}
[] operator.