小编Bat*_*n05的帖子

是否在C++中具有相似功能的TreeSet数据结构

我需要在C++中使用树集数据结构(在Java中可用),并使用像TreeSet.lower(i)和TreeSet.higher(i) - >这样的函数,它返回的元素只是更低,而且比我高在给定的树集中.有STL吗?

编辑:以下是我需要的功能,我想知道如何使用upper_bound和lower_bound函数来执行此操作:

for (int i = 1; i<10; i++) myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90
int k = 50;  // I need 40 and 60
set<int>::iterator itr = myset.find(k);

if (itr != myset.end()) {
    // Found the element
    itr--; // Previous element;
    cout << *(itr); //prints 40
    itr++; // the element found
    itr++; // The next element
    cout << *(itr);  // prints 60
}
Run Code Online (Sandbox Code Playgroud)

c++ java set treeset data-structures

5
推荐指数
1
解决办法
2369
查看次数

这个算术表达式意味着什么:C++中的A + = B ++ == 0;

我来到这个表达式,并且无法理解以下代码段中第3行的含义:

int A=0, B=0;
std::cout << A << B << "\n"; // Prints 0, 0
A += B++ == 0; // how does this exp work exactly? 
std::cout << A << B << "\n";  // Prints 1, 1
Run Code Online (Sandbox Code Playgroud)

A为它添加B,B为Post增加1,"== 0"是什么意思?

编辑:这是实际的代码:

int lengthOfLongestSubstringKDistinct(string s, int k) {
    int ctr[256] = {}, j = -1, distinct = 0, maxlen = 0;
    for (int i=0; i<s.size(); ++i) {
        distinct += ctr[s[i]]++ == 0; // 
        while (distinct > k)
            distinct -= --ctr[s[++j]] == …
Run Code Online (Sandbox Code Playgroud)

c++ arithmetic-expressions post-increment

5
推荐指数
1
解决办法
124
查看次数

在C代码中使用数百万个malloc()和free()的结果?

我最近在接受采访时被问到这个问题.

假设有一个庞大的C程序库,每个程序都有malloc()s和free()s块的数据.如果你的程序中有一百万次调用malloc()free()一次运行,你认为会发生什么?如果给你一个非常大的内存堆存储,你会给你的答案添加什么?

c memory malloc free

5
推荐指数
1
解决办法
146
查看次数