Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need to get size for CString array in C++

Tags:

c++

visual-c++

I have method which receives CString SearchString[] in C++

I would like to get the size of this array to iterate in for loop, if not then could someone suggest how to convert this array to CStringArray.

#include <string>
using namespace std;

void myFunction(HWND shwnd, CString SearchString[], BOOl Visible)
{
    //how do i get the size of "SearchString" here;
   // I do not know how much it is populated, there might be one, two or three strings
}

int main()
{
    CString Header[12];
    BOOL bVisible;
    myFunction(shwnd,Header,bVisible);
    return 0;
}
like image 296
user1782807 Avatar asked Jul 08 '26 01:07

user1782807


2 Answers

You can use a function template to get a handle on the size of any fixed size array:

template<size_t N >
void foo( CString (&SearchString)[N] )
{
  // the length of the array is N
}

So, you could make your function a template:

template<size_t N >
void myFunction(HWND shwnd, CString (&SearchString)[N], BOOl Visible)
{
   // the length of SearchString is N in here
}

Then just call it like this:

int main()
{
    CString Header[12];
    BOOL bVisible; // you might need to initialize this
    myFunction(shwnd, Header, bVisible);
}
like image 191
juanchopanza Avatar answered Jul 09 '26 14:07

juanchopanza


If you can give some code , it will be helpful to understand and give your answer. From your question I guess you have a array of strings and you want to know the size of it. You can use STL vector where you can use string data type and can easily find the size of the vector. I am giving a sample code which can help you.

#include <iostream>
#include <vector>
#include <string>
using namespace std;

void myfunction(vector<string>& searchstring)
{
    int a=searchstring.size();
    cout<<a;
}
int main()
{
    vector<string>searchstring;
    searchstring.push_back("hi");
    searchstring.push_back("hello");
    searchstring.push_back("man");
    searchstring.push_back("man");
    myfunction(searchstring);
    searchstring.clear();
    return 0;
}

here the size of the vector is 4.

like image 28
lukai Avatar answered Jul 09 '26 14:07

lukai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!