• 13
• 14
• 27
• 9
• 9

# Array Functions

This topic is 2621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I had a great idea yesterday, and due to a lot of things I haven't got around to coding anything until now. The idea was to make a chemistry/nuclear physics simulation (well, not a very comprehensive sim, but a sim nonetheless).

Anyways, to get to the point, I need a function that runs through an array of strings (in this case, a list of stable isotopes) and compares each one to another string (the isotope the user is working on). If a match is found, then it exits the function and sets a boolean value to true. Then, it simply writes whether there is a match or not (if the isotope is stable based on the array mentioned).

Its pretty easy to do with for and if statements, but I keep getting stuck on the details (like getting the array length), so I decided to ask here.

What language?

##### Share on other sites

What language?

Oh yeah, its C++, and I'm writing this on the Visual C++ 2010 Express IDE.

##### Share on other sites
presumably you are using std::vector<std::string> stringArray; Then its a simlpe if and for loops like you said :
 for(int i = 0; i < stringArray.size(); i++){ if(stringArray == stringToBeFound){ setBitToTrue(); break; } } 

##### Share on other sites
The Standard C++ Library contains implementations of such algorithms. You are looking for a particular item, the algorithm that does this is std::find:
 #include <string> #include <vector> #include <iostream> #include <algorithm> int main() { typedef std::vector<std::string> Isotobes; Isotobes isotobes = generateSomeIsotobes(); std::string isotobe = getIsotobeToSearchFor(); Isotobes::iterator it = std::find(isotobes.begin(), isotobes.end(), isotobe); bool found = (it != isotobes.end()); if(found) { std::cout << "Found " << isotobe << " at index " << (it - isotobes.begin()) << '\n'; } else { std::cout << "Failed to locate " << isotobe << '\n'; } } 
Using raw arrays is generally a bad idea. They have a fixed size, which you almost never want, and a tendency to "forget" their size information when you try to use them in interesting ways, which forces you to pass the size around with the array. Passing two related values like this can cause subtle synchronisation bugs where the wrong length is accidentally used. In general, you should use a container such as std::vector<>.

Here is a complete example, which uses simple values to show what is going on:
 #include <ctime> #include <string> #include <vector> #include <cstdlib> #include <iostream> #include <algorithm> typedef std::vector<std::string> Isotobes; Isotobes generateSomeIsotobes() { Isotobes result; result.push_back("1"); result.push_back("2"); result.push_back("3"); result.push_back("4"); return result; } int main() { std::srand(std::time(0)); Isotobes isotobes = generateSomeIsotobes(); std::string isotobe = (std::rand() % 2 == 0 ? "5" : "3"); Isotobes::iterator it = std::find(isotobes.begin(), isotobes.end(), isotobe); bool found = (it != isotobes.end()); if(found) { std::cout << "Found " << isotobe << " at index " << (it - isotobes.begin()) << '\n'; } else { std::cout << "Failed to locate " << isotobe << '\n'; } } 
If you run this code multiple times, it should eventually print both messages.