简体   繁体   中英

C++ split std list into two lists

Hey so I'm reasonably new into c++ and I ran into this problem where I want to split one std list of strings into two lists.

For example: list(1,2,3,4) -> list1(1,2) & list2(3,4)

I guess splice is what I am supposed to use for this, but I could not understand how that works at all...

Can someone please advice me how to do this?
Sorry about my bad English and thanks for help everyone.

Try the following

#include <iostream>
#include <list>
#include <string>
#include <iterator>

int main() 
{   
    std::list<std::string> lst1 = { "1", "2", "3", "4" };

    for (const auto &s : lst1 ) std::cout << s << ' ';
    std::cout << std::endl;

    std::cout << std::endl;

    std::list<std::string> lst2;
    lst2.splice( lst2.begin(), 
                 lst1, 
                 lst1.begin(), 
                 std::next( lst1.begin(), lst1.size() / 2 ) );

    for (const auto &s : lst2 ) std::cout << s << ' ';
    std::cout << std::endl;

    for (const auto &s : lst1 ) std::cout << s << ' ';
    std::cout << std::endl;
    return 0;
}

The output is

1 2 3 4 

1 2 
3 4 

The other approach

#include <iostream>
#include <list>
#include <string>
#include <iterator>

int main() 
{
    std::list<std::string> lst1 = { "1", "2", "3", "4" };

    for (const auto &s : lst1 ) std::cout << s << ' ';
    std::cout << std::endl;

    auto middle = std::next( lst1.begin(), lst1.size() / 2 );

    std::list<std::string> lst2( lst1.begin(), middle ); 

    std::list<std::string> lst3( middle, lst1.end() );

    for (const auto &s : lst2 ) std::cout << s << ' ';
    std::cout << std::endl;

    for (const auto &s : lst3 ) std::cout << s << ' ';
    std::cout << std::endl;
    return 0;
}

The output is

1 2 3 4 
1 2 
3 4

"I'm reasonably new into c++"

It's a common misconception of users coming with Java or C# experience, that std::list is the exact behavioral replacement of List in their language. In fact for the mentioned two it's std::vector in c++.

"I want to split half of list into one list and other half to another."

You can easily do this, giving up the std::list , and switch to a std::vector if possible:

#include <iostream>
#include <vector>

void print(const std::string name, const std::vector<int>& v) {
    std::cout << name << " = { ";
    bool first = true;
    for(auto i : v) {
        if(!first) {
            std::cout << ", ";
        }
        else {
            first = false;
        }
        std::cout << i;
    }
    std::cout << " }" << std::endl;
}

int main() {
    std::vector<int> master { 1, 2, 3, 4};

    size_t halfPos = master.size() / 2;

    if(halfPos > 0) {
        std::vector<int> firstPart(master.begin(),master.begin() + halfPos);
        std::vector<int> lastPart(master.begin() + halfPos,master.end());

        print("master",master);
        print("firstPart",firstPart);
        print("lastPart",lastPart);
    }
    return 0;
}

Output:

 master = { 1, 2, 3, 4 }
 firstPart = { 1, 2 }
 lastPart = { 3, 4 }

LIVE DEMO

As mentioned std::list::splice() has a completely different purpose.

If you really need to have a std::list , your only option is to iterate and count. The std::list::iterator doesn't support operations like + .

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