Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use string indexes in c++ arrays (like php)?

Tags:

c++

arrays

How can I use a string index in a c++ array (like in php)?

like image 264
SomeUser Avatar asked Oct 11 '09 14:10

SomeUser


People also ask

Can you use a string as an array index?

Actually, it has very much to do with the question (specifically, yes, you CAN use a string as an index, but not in the obvious way that the original querier wants).

Is string indexing possible in C?

The index() function locates the first occurrence of c (converted to an unsigned char) in the string pointed to by string. The character c can be the NULL character (\0); the ending NULL is included in the search. The string argument to the function must contain a NULL character (\0) marking the end of the string.

Can an index be a string?

Strings are ordered sequences of character data, 00:15 and the individual characters of a string can be accessed directly using that numerical index. String indexing in Python is zero-based, so the very first character in the string would have an index of 0 , 00:30 and the next would be 1 , and so on.

What is an index array in PHP?

PHP indexed array is an array which is represented by an index number by default. All elements of array are represented by an index number which starts from 0. PHP indexed array can store numbers, strings or any object. PHP indexed array is also known as numeric array.


1 Answers

You could use std::map to get an associative container in which you can lookup values by a string index. A map like std::map<std::string, int> would associate integer values with std::string lookup keys.

like image 60
sth Avatar answered Sep 22 '22 16:09

sth