Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6540991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:02:55+00:00 2026-05-25T11:02:55+00:00

I have two lists. Both containing the same values: QStringList filePaths; filePaths.append(C:/backup); filePaths.append(C:/download/file1); filePaths.append(D:);

  • 0

I have two lists. Both containing the same values:

 QStringList filePaths;
 filePaths.append("C:/backup");
 filePaths.append("C:/download/file1");
 filePaths.append("D:");
 filePaths.append("C:/program");
 filePaths.append("C:/download");

 QStringList refinedPaths;

 int size = filePaths.size();

 for(int i = 0; i < size; i++)
 {
   refinedPaths.append(filePaths.at(i));
 }

 for(int i = 0; i < size; i++)
 {
   QString str1 = filePaths.at(i);

   for(int j = 0; j < size; j++)
   {
     QString str2 = filePaths.at(j);

     if(str2 == str1)
     {
       continue;
     }

     if(str2.startsWith(str1))
     {
       refinedPaths.removeAll(str2);
     }
   }
 }

What I’m expecting to happen is:
* Iterate through the strings in list, comparing every item in the list with each other.
* If string1 starts with string2 (string2 is therefore the parent directory of string1)
* remove that string from the ‘refined’ stringlist.

Howver, what is happening is that if(str2.startsWith(str1)) is returning true every time, and refinedPaths.removeAll(str2); doesn’t remove any of the strings in the list.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:02:56+00:00Added an answer on May 25, 2026 at 11:02 am

    Your code works fine, not sure what problem you are referring to. Using your code here’s the output I got

    C:/backup
    D:
    C:/program
    C:/download
    

    A few things to improve,

    1. instead of copying QStringList element by element you can just use the copy constructor i.e copying from filePaths to refinedPaths QStringList refinedPaths(filePaths);

    2. use iterators instead of iterating by size().

      QStringList refinedPaths(filePaths);
      
      for(QStringList::const_iterator itr1 = filePaths.begin(); filePaths.end() != itr1 ; ++itr1)
      {
      
        for(QStringList::const_iterator itr2 = filePaths.begin(); filePaths.end() != itr2 ; ++itr2)
        {
          if(*itr1 == *itr2)
          {
            continue;
          }
      
          if(itr2->startsWith(*itr1))
          {
            refinedPaths.removeAll(*itr2);
          }
        }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two lists, both using the same class (Part). The first list is
I have two lists. The types of both lists inherit from the same base
I have two lists which are guaranteed to be the same length. I want
I have two lists A and B, at the beginning of my program, they
I have two lists ... List<ObjectA> listA List<ObjectB> listB Both have a int property
I have two text blocks, containing company names. Both contain names of hundreds of
I have two dropdown lists, one containing a list of countries and one for
I have two Lists that both are drag-drop enabled. I also draw a chart
I have two lists: eg. a = [1,8,3,9,4,9,3,8,1,2,3] and b = [1,8,1,3,9,4,9,3,8,1,2,3] Both contain
I have two select lists. Both are populated using php and mysql. When the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.