C++ split std list into two lists

爷,独闯天下 提交于 2019-12-25 18:24:46

问题


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.


回答1:


"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 +.




回答2:


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


来源:https://stackoverflow.com/questions/26690083/c-split-std-list-into-two-lists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!