简体   繁体   中英

Check to see if a string array value is null/empty C++

Ok so i want to check to see if the value of the array, array1 is empty and if it is, it should not be put in array2 . How do i do this?

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array1[i].find(result[1]);

            if (found!=std::string::npos)

                array2[i] = array1[i];              
        }   
    cout 

Sounds like you want std::copy_if . I'm not sure what condition you want. In English you describe the C++ function .empty() , but your C++ code implements a test "string contains the substring result[1] ". std::copy_if can work with both.

It looks like you want the string's empty() method. You could expand your condition like this:

if (!array1[i].empty() && found != std::string::npos)
    array2[i] = array1[i];

Obviously that will leave unchanged (presumably blank) values in array2 though, where the array1 values weren't copied across. If that's not what you want then you'll need to keep a second loop counter which will index array2 . It will only be incremented every time you actually copy a value across.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM