Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一些函数的特殊行为需要记录到文档中 #38

Open
activesys opened this issue Feb 27, 2015 · 0 comments
Open

一些函数的特殊行为需要记录到文档中 #38

activesys opened this issue Feb 27, 2015 · 0 comments

Comments

@activesys
Copy link
Owner

容器的insert_range函数的行为,以deque_insert_range为例:

#include <stdio.h>
#include <cstl/cdeque.h>

int main(int argc, char* argv[])
{
    deque_t* pdeq = create_deque(int);
    iterator_t it;
    int i = 0;

    if (pdeq == NULL) {
        return -1;
    }

    deque_init(pdeq);

    deque_push_front(pdeq, 5);
    deque_push_front(pdeq, 4);
    deque_push_front(pdeq, 3);
    deque_push_front(pdeq, 2);
    deque_push_front(pdeq, 1);
    deque_push_front(pdeq, 0);
    deque_push_back(pdeq, 6);
    deque_push_back(pdeq, 7);
    deque_push_back(pdeq, 8);
    deque_push_back(pdeq, 9);
    for (it = deque_begin(pdeq);
         !iterator_equal(it, deque_end(pdeq));
         it = iterator_next(it)) {
        printf("%d ", *(int*)iterator_get_pointer(it));
    }
    printf("\n");

    deque_insert_range(pdeq, iterator_next_n(deque_begin(pdeq), 4),
        deque_begin(pdeq), iterator_next_n(deque_begin(pdeq), 2));
    for (it = deque_begin(pdeq);
         !iterator_equal(it, deque_end(pdeq));
         it = iterator_next(it)) {
        printf("%d ", *(int*)iterator_get_pointer(it));
    }
    printf("\n");

    return 0;
}

它的输出结果是:

0 1 2 3 4 5 6 7 8 9 
0 1 2 3 2 3 4 5 6 7 8 9

而不是想象中的:

0 1 2 3 4 5 6 7 8 9 
0 1 2 3 0 1 4 5 6 7 8 9

其实STL响应的代码也是这样的结果:

#include <iostream>
#include <deque>

int main(int argc, char* argv[])
{
    std::deque<int> deq;

    deq.push_front(5);
    deq.push_front(4);
    deq.push_front(3);
    deq.push_front(2);
    deq.push_front(1);
    deq.push_front(0);
    deq.push_back(6);
    deq.push_back(7);
    deq.push_back(8);
    deq.push_back(9);

    for (std::deque<int>::iterator it = deq.begin();
         it != deq.end();
         ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    deq.insert(deq.begin() + 4, deq.begin(), deq.begin() + 2);
    for (std::deque<int>::iterator it = deq.begin();
         it != deq.end();
         ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出结果:

0 1 2 3 4 5 6 7 8 9 
0 1 2 3 2 3 4 5 6 7 8 9

像这样的行为需要记录到教程或者参考手册中。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant