1

I am naive programmer in c++ and just started learning c++ by solving problems. Please let me know if there is any old link or source where i can get the answer of below question.

I have string array and i need to parse it till the last element but I don't know what condition I will put to check that loop has reached the last element in array.

For example

int fun1(string members[]){
    int i=0;
     for(i=0;END_CONDITION;i++){
         /*Do some work*/
     }
}

And member is something like this

 string members[] = 
 {"BETTY F M A A C C",
  "TOM M F A D C A",
  "SUE F M D D D D",
  "ELLEN F M A A C A",
  "JOE M F A A C A",
  "ED M F A D D A",
  "SALLY F M C D A B",
  "MARGE F M A A C C"};

I have tried to search alot to get the answer but i could't find any answer which can help. In most of thread guys are saying to use vector instead of string array. But here i am getting argument as string array.

Vid
  • 137
  • 1
  • 2
  • 13

5 Answers5

1

In case of plain array you have to pass the size of array to your function. There's no other option. However, you can use vector<string> to make things easier...

ravi
  • 10,994
  • 1
  • 18
  • 36
  • 4
    There is not other *good* option for a beginner, but there are other options: `template int fun1(string (&members)[N]) { ...use N here... }`. You can also use a sentinel value - e.g. an empty `std::string`. – Tony Delroy Nov 13 '14 at 08:15
0

You can do for individual string - myStr.length() it will give you the length of your individual string. myStr.length() - 1 will be your last element index for a string.

For length of a string array an answer has already been posted on Stack Overflow - Find the length of string array with strlen()

I use this - sizeof(myStrArray)/size(myStrArray[0]) to get the length of a string array (i.e. no. of elements) and it works for me. However, you have to be careful if myStrArray is defined with specific linkage scopes e.g. static or extern - this might not work the same way. Also, if you use pointers, this will not be helpful (editd after @MikeSeymour commented on the answer).

This should help you. The other option is to create a vector and store each string element in your vector and you should get the indices out. myVector.size() will give you the no. of elements in your string vector and you can work out which is the last element index based on 0-based indexing :)

#include <iostream>
using namespace std;

int main() {
    std::string s[] =  
 {"BETTY F M A A C C",
  "TOM M F A D C A",
  "SUE F M D D D D",
  "ELLEN F M A A C A",
  "JOE M F A A C A",
  "ED M F A D D A",
  "SALLY F M C D A B",
  "MARGE F M A A C C"};
  size_t myStrArrLen = 0;

  myStrArrLen = sizeof(s)/sizeof(s[0]);

  cout <<  myStrArrLen << endl;
  cout << s[myStrArrLen - 1].length() << std::endl;

  return 0;
}

Does this result show what you wanted to do?

Community
  • 1
  • 1
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
  • @ErikAlapää Does the OP say that he is interested in C-style solution? From the question here, where does the C application come in play? – ha9u63a7 Nov 13 '14 at 08:26
  • No, but a '\0'-terminated string is C-style. And calling it a string array is confusing, a string array is something like std::string string_arr[10], which is seldom used... – Erik Alapää Nov 13 '14 at 08:28
  • 1
    Beware that this `sizeof` malarkey only works on the array itself. If you try it in the function, where you only have a pointer, you'll get the wrong value and weird runtime behaviour. – Mike Seymour Nov 13 '14 at 08:42
  • @MikeSeymour +1 for that reminder. The OP was suggested to use a vector<> but he insisted that there should be another way (not necessarily a better way) to do it. – ha9u63a7 Nov 13 '14 at 08:49
0

C++ doesn't support any feature to determine the size of an array, unless it is allocated on stack, where sizeof may be used. Even with stack allocated array, its size cannot be determined in the function to which it is passed.

You better use vector, list, or array (array for compile-time defined array size). Otherwise, keep the counts yourself via some struct, or other variables.

Ajay
  • 18,086
  • 12
  • 59
  • 105
0

sizeof(string) is not been change according to string::length.

that's why you can use sizeof(members)/sizeof(string)

A complete solution:

int main()
{
     string members[] =
     {"BETTY F M A A C C",
      "TOM M F A D C A",
      "SUE F M D D D D",
      "ELLEN F M A A C A",
      "JOE M F A A C A",
      "ED M F A D D A",
      "SALLY F M C D A B",
      "MARGE F M A A C C"};

     cout<<sizeof(members)/sizeof(string)<<endl;
}

This will give output: 8

inside the function you can't do so, because in:

int fun1(string members[]){
   cout<<sizeof(members)<<endl;
}

you'll get size of a pointer, not the size you've got in the upper example.

So you just should add size to your function:

int fun1(string members[], size_t size)

And from the main function above you should call it like this:

size_t size = sizeof(members)/sizeof(string);
fun1(members,size);
SHR
  • 7,940
  • 9
  • 38
  • 57
0

You say:

In most of thread guys are saying to use vector instead of string array. But here i am getting argument as string array.

Can you change what you receive as arguments? Consider these alternatives:

int fun1(string members[], std::size_t length){
    for(std::size_t i=0; i < length; i++) {
        /*Do some work*/
    }
}
string members[] = 
{ "BETTY F M A A C C", // ...
  "MARGE F M A A C C" };
fun1(members, 2);

Or:

template<typename I>
int fun1(I begin, const I& end) {
    for(; begin != end; ++begin)
        /* Do some work with (*begin) */
    }
}
fun1(std::begin(members), std::end(members));

Or:

int fun1(const std::vector<std::string>& members)
    for(const auto& s: members) {
        // on n-th iteration, s is a const reference to n-th element
    }
}

std::vector<std::string> members = 
{ "BETTY F M A A C C", // ...
  "MARGE F M A A C C" };
fun1(members);

Or the same as the last example, with std::array instead of std::vector.

utnapistim
  • 26,809
  • 3
  • 46
  • 82