How to check to see if an element in vecto contains a certain value

Hey,
I am working on this project where I am storing info from status messages in a vector. Once elements are stored I am reading the vector and displaying them in a list ctrl. However, if the element contains a time the element needs to be diplayed in the first column(which is Time). So I am trying to check to see if the element is in the following format"hh:mm:ss", if so it is a time value. Well, I have searched and have not found anything that allows me to do this. By any chance is there a way to do this. Any help will be greatly appreciated.

Thanks In Advance
You could test it step by step. First check the length == 8 characters.
Next check the existence of the colon ':' at positions 2 and 5.

If the above is true, it's already quite probable that you have a time element. But you could finish the checking by verifying that the other characters are all numeric digits, maybe by using isdigit()
http://www.cplusplus.com/reference/cctype/isdigit/
Last edited on
Chervil,
Thanks. I fully understand what you are saying but I am having a hard time to figure out how to do this with vectores. This is my first time working with vectors. This is what I have tried but failed:

1
2
3
4
5
6
7
8
9
10
for(vector<CString>::iterator it = myvector.begin(); it != myvector.end(); it++)
{
  //Trying to check lenght for each element in vector
  if(myvector[it].GetLength==8)
  {

  // do checks
  }
}


In the code above I get build errors about using GetLength. I am not sure how to retrieve each element and perfrom the checks you mentioned.
I'm not familiar with the data type CString

Edit: got it.
Last edited on
I am using C++ MFC. To my understanding you have to use CString in MFC.
It looks as though you need to use
if (it->GetLength() == 8)
and also whatever other methods apply for the string type you are using.

Either of these should access a specific character
1
2
    it->GetAt(2)
    (*it)[5]

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

int main(){

	std::vector<std::string> date = {"3",":","3",":","03"};

	/*to access elements -->(date[0] == '3') .. (date[1] == ':') .. (date[2] == '3') .. (date[3] == ':')
	  and (date[4] == "03")
	*/
	date[4] = "05";     

        /* i just checked and this works to.. */
	
	date[4][0] = '4';        
 
        //soo in c++11 this will output the vector...
	for(auto i: date) std::cout<<i;

}


if you can use c++11, compile with (-std=c++11 with g++), if not this won't work..
Last edited on
Chervil,
Thanks for the above input. The GetLength() is working, but checking everything else isn't. For example if I have a vector with an element equal to,"10:25:34". I am having a problem with checking to see if that element is in the following format hh:mm:ss. I can't check the index because, well I don't think I can because it is all one element. Just checking to see if the length is 8 isn't going to do because a status can be just a length of 8. Any help will be greatly appreiated. Thanks In Advance.
Chervil,
Thanks. I am now able to check for ':' by using GetAt() or *it[index], but am having problems with checking to see if other indexs is a digit.
I have used the following to check to see if it is a digit:
1
2
3
4
5
6
if(isdigit(it->GetAt(2))
{

  //do something
}


1
2
3
4
5
6
7

if((*it)[2] == ':')
{

  //do something
}


I am not getting errors but my check is not true which it should be.Any help will be greatly appreiated. Thanks In Advance.
Well, I think the isdigit() test should be done for all the positions except for 2 and 5;

You could put all the validation in a separate function, like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool checkDate(CString str)
{
    if (str.GetLength() != 8)
        return false;
        
    for (int i=0; i<8; i++)
    {
        if (i == 2 || i==5)
        {
            if (str[i] != ':')
                return false;
        }
        else
        {
            if (!isdigit(str[i]))
                return false;
        }
    }
    
    return true;
}


... and call that function like this:

1
2
3
4
5
6
7
8
9
for(vector<CString>::iterator it = myvector.begin(); it != myvector.end(); it++)
{
    if (checkDate(*it))
    {
        // yes we have a date
    }


}

I've not tested this code, so there may be errors.
Last edited on
Thanks. Got it working!!!!
Topic archived. No new replies allowed.