C++ - 令牌之前的预期主表达式'('并且缺少"("标记之前的模板参数
C++ - Expected primary expression before '(' token AND missing template arguments before ‘(’ token
我定义
typedef std::map< int, std::set<int> > SparseMap;
然后我试着用这种方式插入一对:
pair<SparseMap::iterator,bool> result;
result = sparseBlue.insert(SparseMap::value_type(row, set(col)) ); //errors
if(result.second)
cout << "Inserted" << endl;
- 行和列是整数矩阵坐标
- 稀疏蓝色声明为
SparseMap sparseBlue;
为什么我在插入的行中出现这些错误?
我相信@T.C和@Lightness Races在轨道上使用std::set<int>
的想法是正确的。唯一的问题是std::set<T>
没有一个接受构造函数的构造函数,该构造函数接受t类型的单个项(在本例中为int)。
假设你真的需要一个集合作为地图中的值,那么你可能想要这样的东西:
std::set<int> tmpSet;
tmpSet.insert(col);
result = sparseBlue.insert(std::make_pair(row, tmpSet));
另一个解决方案是,您可以在添加项目之前插入到地图中:
#include <map>
#include <set>
using namespace std;
int main()
{
int row = 0;
int col = 0;
std::map<int, set<int>> sparseBlue;
// insert empty item
auto iter = sparseBlue.insert(std::make_pair(row, std::set<int>()));
// did a new item get inserted?
cout << "The item did " << (iter.second?"":"not") << " get insertedn";
// add item to set
(*iter.first). // the map iterator
second. // the set
insert(col); // what we want to do
}
std::map::insert的返回值返回一个std::pair
,表示插入项的迭代器,以及true
或false
,具体取决于是否插入了新项。
实例
相关文章:
- 概念中的cv限定符需要表达式参数列表
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 表达式 SFINAE:如何根据类型是否包含具有一个或多个参数的函数来选择模板版本
- 使用自动推导的 lambda 参数作为常量表达式
- 编译器是否强制根据模板参数计算表达式?
- 错误:参数中无效地使用了无效表达式
- 将折叠表达式传递给可变参数模板
- 为什么带有指针子对象的文字类类型的 constexpr 表达式不能是非类型模板参数
- C++: priority_queue:模板参数中的 lambda 表达式
- 容器如何处理 lambda 表达式的参数
- 在C++ Lambda 表达式中,为什么人们更喜欢按值捕获而不是作为参数传递?
- 整体模板参数。错误:在常量表达式中使用'this'
- 我可以std::在fold表达式中转发参数吗
- 折叠表达式模板参数推导/替换失败
- 折叠表达式和参数包:static_assert 内 Args&& 和 Args 之间的区别
- C++:从捕获函数参数的函数返回 lambda 表达式
- 是否可以使用带有模板化参数的特征块表达式作为左值?
- 编译器在传递 const 变量时返回错误:模板参数不是常量表达式
- 如何在C++中将 lambda 表达式作为参数传递
- 导出模板参数表达式的类型