Creating a searchable array of strings

Hello!

I've been trying to create an array that holds two pieces of data and allows the first column to be searched but print out the second column. I've seen a few examples but I'm still stuck. Could someone show me what the array looks like if the data is

1234 example info
2345 example info
4567 example info

I think I can figure out how to search the array but I'm just not sure how to set it up.

Thank you!
This is what I'm trying to do:

1
2
3
4
5
string treeArray[5][2] = { { "1234",  "example info"}, 
			{ "2345",  "example info" },
			{ "3456",  "example info" },
		        { "4567",  "example info" },
			{ "5678",  "example info" } };


Is this correct? I'm getting compile errors when I try to do it this way. This is what I have for my search code:

1
2
3
4
5
6
7
8
9
10
11
   
 std::string* currentElement = &(treeArray[5][2]); // Create pointer to first element

    for (int i=0; i < HEIGHT*WIDTH; i++)
{
      if (*currentElement == treeData)
  { std::cout << "Value exists" << std::endl;
    break;
  }
  currentElement++;
}
Last edited on
Is this correct? I'm getting compile errors when I try to do it this way.

No, this is not correct. For starters, treeArray[5][2] is out of the bounds of the treeArray, so assigning it's address to currentElement is a nonstarter.

I would second mbozzi's recommendation to use std::map, but if you must use a two dimensional array, consider using typedefs or using declarations to make your code easier to read and write.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// https://ideone.com/gXLl3V
#include <iostream>
#include <string>


int main() {
    using leaf_type = std::string[2];

    leaf_type tree[5] =
    {
        {"1234", "1234-abcde"},
        {"2345", "2345-bcdef"},
        {"3456", "3456-cdefg"},
        {"4567", "4567-defgh"},
        {"5678", "5678-efghi"},
    };

    auto data = "3456";

    for (auto* current = &tree[0]; current < tree+5; ++current) 
    {
        if ((*current)[0] == data) {
            std::cout << data << " exists with value " << (*current)[1] << '\n';
            break;
        }
    }
}
Last edited on
I haven't used map before, that looks interesting. Thanks for the suggestion as well cire, that really helped me out. I think I'll see if I can get it working with the array and then I'll see if I can figure out map.

Thanks!

Autumn
I seem to be getting a build error stating invalid types for array subscript, could you possibly take a look at my code. I changed it a little bit because my compiler wasn't accepting the code with using leaf_type = std::string[2]. I know this seems to be more of a compiler issue but does the code look okay to you?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main()
{
    std::string tree[5][2] =
    {
        {"1234", "1234-abcde"},
        {"2345", "2345-bcdef"},
        {"3456", "3456-cdefg"},
        {"4567", "4567-defgh"},
        {"5678", "5678-efghi"},
    };

    int treeForm();
    int treeData;
    treeData=treeForm();


    int data = treeData;

    for (int* current = &tree[0]; current < tree+5; ++current)
    {
        if ((*current)[0] == data)
        {
            std::cout << data << " exists with value " << (*current)[1] << '\n';
            break;
         }
    }
    std::cout << treeData << " End of program.";
}


Thank you
Please forget about pointers. There is no need for them in modern C++ for beginners.
Might be different for advanced programmers.

Easy way to deal with 2D arrays:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
  const int ROW_COUNT = 5;
  const int COL_COUNT = 2;

  std::string tree[5][2] =
  {
    { "1234", "1234-abcde" },
    { "2345", "2345-bcdef" },
    { "3456", "3456-cdefg" },
    { "4567", "4567-defgh" },
    { "5678", "5678-efghi" },
  };

  for (int row = 0; row < ROW_COUNT; row++)
  {
    for (int col = 0; col < COL_COUNT; col++)
    {
      cout << tree[row][col] << "\t";
    }
    cout << "\n";
  }
}
Thanks for the tip Thomas, it seems to be printing out all the strings though. Would you recommend putting an if loop in to check for the value I'm looking for? I don't really know how to combine your code with cire's to make the array searchable. Thanks again
closed account (E0p9LyTq)
Your data is structured to work well with a C++ multiset or multimap container, searches for specified keys done with the <algorithm> C++ library.
Use std::map http://en.cppreference.com/w/cpp/container/map
http://en.cppreference.com/w/cpp/container/map/find

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <map>

int main()
{
    const std::map< std::string, std::string > tree
    {
        {"1234", "1234-abcde"},
        {"2345", "2345-bcdef"},
        {"3456", "3456-cdefg"},
        {"4567", "4567-defgh"},
        {"5678", "5678-efghi"}
    };

    // search for key "3456"
    const std::string key = "3456" ;
    const auto iter = tree.find(key) ;
    if( iter != tree.end() ) std::cout << "found: '" << iter->second << "'\n";
    else std::cout << "key '" << key << "' not found\n" ;
}

http://coliru.stacked-crooked.com/a/f405db3e45ccc3cf
Searching is easy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
  const int ROW_COUNT = 5;
  const int COL_COUNT = 2;

  std::string tree[5][2] =
  {
    { "1234", "1234-abcde" },
    { "2345", "2345-bcdef" },
    { "3456", "3456-cdefg" },
    { "4567", "4567-defgh" },
    { "5678", "5678-efghi" },
  };
  string needle = "3456";
  for (int row = 0; row < ROW_COUNT; row++)
  {

    if (tree[row][0] == needle)
    {
      cout << "Found " << needle << " at row " << row << "\n\n";
    }
  }
}
This seems to be something I could use in my program :D

Thanks for all the replies! Super helpful :)
Topic archived. No new replies allowed.