标签: std

标识符“迭代器”的语法错误

以下代码在 VS 2013 中引发两个编译器错误:

  1. 模板函数定义抛出

    错误 C2061:语法错误:标识符“ iterator

  2. 该模板函数的特化会抛出异常

    错误 C2912:显式特化“ double getFillIn<double,double>(fillInOptions,double,std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,inputLoader *,va_list)”不是函数模板的特化

谁能解释一下为什么吗?我相当确定第二个错误只是第一个错误的结果,但我不明白为什么它无法找出该iterator标识符。

#include <map>

template <typename T> class table {
};

template <typename S, typename T>
void f(S s, std::map<S, table<T>*>::iterator it);
Run Code Online (Sandbox Code Playgroud)

c++ syntax dictionary iterator std

-2
推荐指数
1
解决办法
3963
查看次数

C++,为什么数组比向量更快并且使用更少的内存

在一道leetcode题中。当我用来std::vector存放东西的时候。 i_max、 和j_max是整数。

vector<int> left_vec(i_max);
vector<int> right_vec(j_max);
Run Code Online (Sandbox Code Playgroud)

运行时间:100ms,内存使用:71.5MB。

当我用来std::array存放东西的时候。

int left_vec[i_max];
int right_vec[j_max];
Run Code Online (Sandbox Code Playgroud)

运行时间:40ms,内存使用:16.1MB。

代码的其他部分完全相同,唯一的区别是使用向量或数组。

我很困惑为什么会这样。

c++ memory runtime std

-2
推荐指数
1
解决办法
1067
查看次数

相同的模板结构,C++

考虑我有结构 RGB 和 ARGB。

template<typename T>
struct RGB {
    T r,g,b;
};

template<typename T>
struct ARGB {
    T a,r,g,b;
}
Run Code Online (Sandbox Code Playgroud)

现在我通过以下来定义它们。

using RGB_888 = RGB<unsigned char>;
using RGB_Float = RGB<float>;
using ARGB_8888 = ARGB<unsigned char>;
using ARGB_Float = ARGB<float>;
Run Code Online (Sandbox Code Playgroud)

在某些时候,我想从一个 rgb 转换为另一个,从 rgb 转换为 argb。所以我做以下事情。

template<typename Source, typename Dest>
void convert(const Source& source, Dest& dest)
{

}
Run Code Online (Sandbox Code Playgroud)

它会像这样工作。

RGB_888 rgb888{123,22,311};
RGB_Float rgbFloat;
convert<RGB_888,RGB_Float>(rgb888,rgbFloat);

RGB_888 rgb888(123,22,132};
ARGB_Float argbFloat;
convert<RGB_888,ARGB_Float>(rgb888,argbFloat);
Run Code Online (Sandbox Code Playgroud)

问题是我无法检测typename Sourcetypename Dest是否来自相同的颜色模型,我需要正确转换。换句话说,如果一些typename …

c++ templates std is-same

-2
推荐指数
1
解决办法
80
查看次数

为什么 C++ std 库本质上不是线程安全的?

我知道,从根本上讲,在 C++ 中使用类不是线程安全的,因为它们的函数不是原子的,这可能会导致竞争条件。

在学习在类中编写多线程程序时,我们必须包装在互斥体中读取/写入对象的关键部分,以确保原子性。在花时间阅读了一些标准库之后,我很好奇为什么 C++ 不自动为用户执行此操作。

我的理解是,互斥体只包含一个布尔值或整数来表示锁的状态,以及某种形式的队列来表示等待锁的所有线程。鉴于这并不是很大的开销,为什么标准库不自动提供这种抽象并用仅该类本地的互斥体(即不是全局互斥体)包装其所有函数?

c++ multithreading std

-2
推荐指数
1
解决办法
209
查看次数

std :: sort c ++什么是二进制函数

我正在阅读c ++参考站点.

我不明白这是什么意思?给你解释它的意思和例子

comp二进制函数,它接受范围中的两个元素作为参数,并返回一个可转换为bool的值.返回的值表示作为第一个参数传递的元素是否被认为是在它定义的特定严格弱顺序中的第二个参数之前.该函数不得修改其任何参数.这可以是函数指针或函数对象.

c++ sorting binary function std

-3
推荐指数
1
解决办法
1722
查看次数

调用std :: sort

sort 在C++标准库中被称为:

sort (first element, last element);
Run Code Online (Sandbox Code Playgroud)

所以,如果我有一个数组:

int a[n];
Run Code Online (Sandbox Code Playgroud)

我应该称之为sort:

sort(&a[0], &a[n-1]);
Run Code Online (Sandbox Code Playgroud)

因为a[0]是第一个元素,a[n-1]也是最后一个元素.但是,当我这样做时,它不会对最后一个元素进行排序.要获得完全排序的数组,我必须使用:

sort(&a[0], &a[n]);
Run Code Online (Sandbox Code Playgroud)

为什么是这样?

c++ sorting std

-3
推荐指数
1
解决办法
295
查看次数

std :: set Unique Pointer

struct departure_compare {
    bool operator() (const Leg* lhs, const Leg* rhs) const
    {
        return lhs->CurrentDepartureTime() < rhs->CurrentDepartureTime();
    }
};

class Station
{
    uint station_number_;
    std::set<Leg *, departure_compare> departure_legs_in_order_; // legs that depart from this station in order of departure time
public:
    Station(uint station_number) : station_number_(station_number) {};
    void addDepartureLeg(Leg *leg) { departure_legs_in_order_.insert(leg); };
    const std::set<Leg *, departure_compare>& DepartureLegs() const { return departure_legs_in_order_; };
    uint StationNumber() { return station_number_; };
};
Run Code Online (Sandbox Code Playgroud)

我把它称为循环

Leg *new_leg = new Leg();
start_station->addDepartureLeg(new_leg); // start_station of type …
Run Code Online (Sandbox Code Playgroud)

c++ pointers std

-3
推荐指数
1
解决办法
174
查看次数

如何将类型向量<tuple <string,int,int >>打印到屏幕c ++?

让我说我有

std::vector<std::tuple<string ,int ,int>> tupleVector;
tupleVector.push_back(std::tuple<string ,int ,int>("Joe", 2, 3));
tupleVector.push_back(std::tuple<string ,int ,int>("Bob", 4, 5));
Run Code Online (Sandbox Code Playgroud)

如何迭代向量以打印包含元组的此向量的所有值?

c++ tuples vector std

-4
推荐指数
1
解决办法
2868
查看次数

g ++抱怨STD功能

我有一段下载的源代码,当尝试使用g ++编译器通过Cygwin进行编译时,编译器给出了一个错误,指出'transform'函数在此范围内未声明...

我正在使用std命名空间,我有正确的标头.我不确定它为什么不编译..语法看起来正确

这是代码块部分.

string tolower (const string & s)
  {
    string d = s;
    transform(d.begin(), d.end(), d.begin(), (int(*)(int)) tolower);
    return d;
  }  // end of tolower
Run Code Online (Sandbox Code Playgroud)

这是我的标题部分:

#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>

// standard library includes ...

#include <string>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <sstream>
#include <ios>
#include <iterator>

using namespace std; 
Run Code Online (Sandbox Code Playgroud)

c++ g++ std

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

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

我应该包括什么来抛出bad_function_call异常?

主题中的问题.我试过这个:

throw new std::bad_function_call("!");
Run Code Online (Sandbox Code Playgroud)

得到下一个错误:

错误C2039:'bad_function_call':不是'std'的成员

错误C2061:语法错误:标识符'bad_function_call'

c++ exception std

-6
推荐指数
1
解决办法
858
查看次数

为什么这个简单的C++代码段错误?

这是标题:

#include <string>

template <typename L>
class A
{
    L l;

public:
    A() : l("a-text") {}
    const std::string get() const { l.get(); } // <<<< Edit: missing return!
};
Run Code Online (Sandbox Code Playgroud)

这是a.cpp:

#include "a.h"
#include <iostream>

class L {
    const std::string v;

public:
    L(const std::string& v_): v(v_) {}
    const std::string get() const { return v; }
};

int main() {
    L l("l-text");
    std::cout << l.get().c_str() << std::endl;

    A<L> a;
    std::cout << a.get().c_str() << std::endl; // <<<< - this …
Run Code Online (Sandbox Code Playgroud)

c++ templates std

-6
推荐指数
1
解决办法
104
查看次数