小编Mr.*_*C64的帖子

为什么使用像这样的find_if失败?

我想从地图中找到第一个非零元素,因此我做了以下代码:

#include <map>
#include <iostream>
#include <algorithm>

bool nonzero(std::map<char,int>::const_iterator& it);

int main()  {
    std::map<char, int> m;
    m['a'] = 0;
    m['b'] = 1;
    std::map<char,int>::iterator it = std::find_if(m.begin(), m.end(), nonzero);
    std::cout << it->first << '\t' << it->second << std::endl;
    return 0;
}


bool nonzero(std::map<char,int>::const_iterator& it)    {
    return it->second;
}
Run Code Online (Sandbox Code Playgroud)

g ++给出了非常复杂的错误,并说:

/usr/include/c++/5/bits/predefined_ops.h:234:30: error: invalid initialization of reference of type ‘std::_Rb_tree_const_iterator<std::pair<const char, int> >&’ from expression of type ‘std::pair<const char, int>’
  { return bool(_M_pred(*__it)); }
Run Code Online (Sandbox Code Playgroud)

我不明白它说的是什么以及为什么我的程序会失败.

c++ dictionary stl find stl-algorithm

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

比较iterator和const_iterators

我有一个方法在地图中找到特定的位置,并通过迭代器引用'返回':

bool Func(const int searchKey, MyMap::iterator& iter) const {
    iter = _map.upper_bound(searchKey);  // Compiler error: comparing non-const iterator and const iterator

    const bool found = iter != _map.begin();

    if(something){
        --_map;
        return true;
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

我收到编译器错误,因为std::upper_bound()它返回std::const_iterator并将其与a进行比较std::iterator.

我应该'转换'非常量的返回值upper_bound()吗?

c++ iterator stl const stdmap

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

BSTR 内存泄漏到 wstring

考虑以下代码,这是否存在内存泄漏?

HRESULT GetPath(wstring &outDomainPath)
{
    CComBSTR bstrDomainPath;

    AnotherGetPath(&bstrDomainPath);
    outDomainPath = bstrDomainPath.Detach();
}
Run Code Online (Sandbox Code Playgroud)

这有什么区别?(还是内存泄漏??)

HRESULT GetPath(wstring &outDomainPath)
{
    CComBSTR bstrDomainPath;

    AnotherGetPath(&bstrDomainPath);
    outDomainPath = bstrDomainPath;//removed detach
}
Run Code Online (Sandbox Code Playgroud)

c++ com memory-leaks bstr

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

使用 Visual Studio 面向 Windows XP 的最新 C++ 标准是什么?

Visual Studio 2019 似乎对 C++17 有很好的支持。不幸的是,用它构建的二进制文件似乎需要在目标计算机上安装通用 CRT,而 UCRT 支持的最低操作系统是 Vista。

因此,如果我想构建一个针对Windows XP 的二进制文件,我必须在UCRT之前使用 VS C++ 编译器吗?是VS 2013吗,它对C++11有一些支持?

c++ windows-xp crt visual-studio visual-c++

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

rand()和Mersenne Twister C++实现

我需要实现一个好的RNG,我猜Mersenne Twister可能对我有好处.我没有找到任何可行的C++实现,我是一个糟糕的谷歌搜索者还是真的不容易找到?!我以前尝试过rand():

srand((unsigned)time(0));
for (int i = 0; i < 9; i++) {
     random =  rand();
     cout<<random;
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么,但随机变量总是相同的数字.....但是如果我添加一个Sleep(1000),它的工作原理!像这样:

srand((unsigned)time(0));
for (int i = 0; i < 9; i++) {
    random =  rand();
    cout<<random;
    Sleep(1000);
}
Run Code Online (Sandbox Code Playgroud)

所以我决定尝试Mersenne Twister ......有人能找到一个解决方案(因为我需要找到非常大量的随机数,所以我不能使用Sleep(1000),这需要时代!)或者帮助我实现Mersenne Twister或者也许是另一个好的RNG.谢谢,抱歉我的英语不好......

c++ random

0
推荐指数
1
解决办法
1360
查看次数

有一个PCWSTR并需要它是一个WCHAR []

我正在从我下载的一些代码中重写一个C++方法.该方法最初将PCWSTR作为参数,然后提示用户输入文件名.我修改了方法以获取两个参数(均为PCWSTR)而不是提示用户.我已经在其他地方生成了文件列表.我试图用我的迭代文件列表的方法中的两个参数调用我的新(修改)方法.

原始方法使用StringCBGetsW命令提示用户输入.像这样...

HRESULT     tst=S_OK;         //these are at the top of the method
WCHAR       fname[85] = {0};  //these are at the top of the method
tst = StringCbGetsW(fname,sizeof(fname));
Run Code Online (Sandbox Code Playgroud)

wchar fname被传递给另一个迭代方法.当我看到那个方法时,它说它是LPCWSTR类型; 我假设它可以取代WCHAR.

但它不能做的就是采用该方法获得的PCWSTR.我的最终目标是尝试不提示用户输入文件名,而是取代之前在另一种方法中迭代的文件名.

TL;博士.我有一个PCWSTR,它需要转换为WCHAR.我不知道WCHAR []是什么或如何用它做什么.包括尝试做printf以查看它是什么.

PS ...我知道有更简单的方法来移动和复制文件,有一个原因我试图使用程序来完成这项工作.

c++ string winapi wchar-t lpcwstr

0
推荐指数
1
解决办法
614
查看次数

将字符串插入另一个字符串...(sprintf)

当我编译这样的东西时,我收到警告......

std::string something = "bacon";

sprintf("I love %s a lot", something.c_str());
Run Code Online (Sandbox Code Playgroud)

它说"警告:从字符串常量到'char*'的已弃用转换.我尝试将文本转换为...

const char *
Run Code Online (Sandbox Code Playgroud)

相反,但我得到一个不同的错误.如果有更好的选择,我不会致力于sprintf.

c++ string printf

0
推荐指数
1
解决办法
545
查看次数

C++ array.at(i)和array [i]表演

在C++中,迭代arrayvector使用array.at(i)函数中的元素或使用array[i]?是否更好(性能方面)?一个比另一个更有效吗?

c++ arrays performance loops

0
推荐指数
2
解决办法
101
查看次数

矢量中的push_back是否插入到同一位置?

class Example
{
    public: int i;
    Example(const Example &e)
    {
        i = e.i;
    }
    Example(int i)
    {
        this->i = i;
    }
};

int main()
{
std::vector<Example*> vec;
std::vector<Example*> newVec;

Example* ex1 = new Example(1);
Example* ex2 = new Example(2);

vec.push_back(ex1);
vec.push_back(ex2);

//newVec = vec; --> This does shallow copy

for(int i=0; i<vec.size(); ++i) // --> Deep copy
{
    Example newE(*(vec[i]));
    newVec.push_back(&newE);
}

for(int i=0; i<newVec.size(); ++i)
{
    std::cout << "\nfoobar" << newVec[i]->i << "\n";
}
}
Run Code Online (Sandbox Code Playgroud)

上面的代码打印两次foobar2.它不应该打印foobar1和foobar2?另外,这是复制包含对象的矢量的最佳方法吗?我想深刻复制.

c++ pointers vector

0
推荐指数
1
解决办法
154
查看次数

C++ 地图容器erase() 分段错误

我使用此代码从地图容器中删除等于某个 int 的元素。

for(auto x:m){
  if((x.second)==element)m.erase(x.first);
}
Run Code Online (Sandbox Code Playgroud)

结果是分段错误。我也试过这个:

for(map<int,int>::iterator i=m.begin();i!=m.end();i++){
  if((i->second)==element)m.erase(i);
}
Run Code Online (Sandbox Code Playgroud)

结果一样。如果将 i++ 放入 if/else 程序将冻结/循环或其他什么。我怎样才能解决这个问题?

c++ stdmap

0
推荐指数
1
解决办法
225
查看次数