标签: stdmap

C++ std ::模板类值的映射

我试图声明一个Row和一个Column类,Row有一个私有std::map值指向模板Column.像这样的东西:

template <typename T> class DataType {
  private:
    T type;
};
template <typename T> class Field {
  private:
    T value;
    DataType<T> type;
};
class Row {
  private:
    std::map<unsigned long,Field*> column;
}; 
Run Code Online (Sandbox Code Playgroud)

好吧,我原则上认为Row类不应该知道我们想要使用哪种Field(或Column),即它是第1 Field<int>列还是第Field<double>2列.但是我不确定什么是正确的语法对于Row::column声明,或者如果std::map它在这个意义上是有限的,我应该使用其他东西.

我建议你提出建议,并提前感谢你们.

c++ methods templates stdmap

13
推荐指数
1
解决办法
2万
查看次数

如何在std :: map中停止从int到float的自动转换,反之亦然

std::map在这里写了一个小程序,如下所示.

int main()
{
  map<int,float>m1;
  m1.insert(pair<int,float>(10,15.0));   //step-1
  m1.insert(pair<float,int>(12.0,13));   //step-2
  cout<<"map size="<<m1.size()<<endl;    //step -3
Run Code Online (Sandbox Code Playgroud)

我创建了一个地图,其中int类型为键,浮点类型为地图m1的值(键 - 值)对

  1. 创建一个普通的int-float对并插入到map中.

  2. 创建了一个cross float-int对并插入到map中.现在我知道隐式转换正在使这对插入映射.

在这里,我只是不希望发生隐式转换,并且应该给出编译器错误.

在我们尝试执行step-2类型操作时,我必须在此程序/映射中进行哪些更改才能使comipiler标记出错?

c++ dictionary stdmap std c++-standard-library

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

map <string,string>如何在此地图中插入数据?

我需要以键值格式存储字符串.所以我正在使用如下的地图.

#include<map>
using namespace std;
int main()
{
    map<string, string> m;
    string s1 = "1";
    string v1 = "A";

    m.insert(pair<string, string>(s1, v1)); //Error
}
Run Code Online (Sandbox Code Playgroud)

我在插入行时遇到错误

错误C2784:'bool std :: operator <(const std :: _ Tree <_Traits>&,const std :: _ Tree <_Traits>&)':无法推断'const std :: _ Tree <_Traits>&'的模板参数来自'const std :: string'

我也尝试过make_pair函数,但是这也报告了同样的错误.

m.insert(make_pair(s1, v1));
Run Code Online (Sandbox Code Playgroud)

请告诉我什么是错的,以及解决上述问题的方法是什么.解决了上述问题后,我可以使用下面的方法来检索基于密钥的值

m.find(s1);
Run Code Online (Sandbox Code Playgroud)

c++ stl stdmap stdstring

12
推荐指数
4
解决办法
7万
查看次数

std :: map with std :: weak_ptr key

我有一个关于使用std :: weak_ptr作为std :: map的键的问题.

#include <map>
#include <memory>

int main()
{
    std::map< std::weak_ptr<int>, bool > myMap;

    std::shared_ptr<int> sharedptr(new int(5));
    std::weak_ptr<int> weakptr = sharedptr;

    myMap[weakptr] = true;

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

上面的程序没有构建,并且尝试编译会给出许多错误消息,例如:

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::tr1::weak_ptr<_Ty>'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(1885) : see declaration of 'std::operator <' …
Run Code Online (Sandbox Code Playgroud)

c++ dictionary weak-references stdmap weak-ptr

12
推荐指数
2
解决办法
4761
查看次数

为地图分配值的最有效方法

将值赋给地图的哪种方式最有效?或者它们都针对相同的代码进行了优化(在大多数现代编译器上)?

   // 1) Assignment using array index notation
   Foo["Bar"] = 12345;

   // 2) Assignment using member function insert() and STL pair
   Foo.insert(std::pair<string,int>("Bar", 12345));

   // 3) Assignment using member function insert() and "value_type()"
   Foo.insert(map<string,int>::value_type("Bar", 12345));

   // 4) Assignment using member function insert() and "make_pair()"
   Foo.insert(std::make_pair("Bar", 12345));
Run Code Online (Sandbox Code Playgroud)

(我知道我可以进行基准测试并检查编译器输出,但是这个问题现在出现了,我手边的唯一一件事就是我的手机......呵呵)

c++ dictionary stdmap std c++-standard-library

12
推荐指数
1
解决办法
3万
查看次数

在C++ 17中,为什么关联容器有一个`erase`成员函数,它接受(非`constst`)`iterator`?

参见,例如, http ://en.cppreference.com/w/cpp/container/map/erase

在C++ 03中有三个重载:

void erase( iterator pos );
void erase( iterator first, iterator last );
size_type erase( const key_type& key );
Run Code Online (Sandbox Code Playgroud)

在C++ 11中,第一和第二过载改变为采取const_iterator,使得它们可与任一被调用iteratorconst_iterator.第一次重载也通过在删除之后将迭代器返回到元素来改进:

iterator erase( const_iterator pos );
void erase( const_iterator first, const_iterator last );
size_type erase( const key_type& key );
Run Code Online (Sandbox Code Playgroud)

在C++ 17中,重新引入了非const重载:

iterator erase( const_iterator pos );
iterator erase( iterator pos );
void erase( const_iterator first, const_iterator last );
size_type erase( const key_type& key );
Run Code Online (Sandbox Code Playgroud)

为什么需要这个?它不加入为不等erase,也不是为insert …

c++ iterator stdmap const-iterator c++17

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

C++里面的变量范围试试

考虑以下代码:

try {
    const Asdf &a = map1.at(index1);
    const Bsdf &b = map2.at(index2);
} catch(std::out_of_range&) {
    return false;
}
// <code>
std::cout<<a[b[42]]; // May throw std::out_of_range which should not be caught here.
return true;
Run Code Online (Sandbox Code Playgroud)

<code>使用a和b.我有两个选择:

  • 放入<code>try块
  • 在try块中获取指针,然后取消引用它们

第一个选项是错误的,因为如果<code>抛出std::out_of_range该函数将返回false,这应该仅在映射查找失败时发生.

第二个选项可能有点难看:

const Asdf *a;
const Bsdf *b;
try {
    a = &map1.at(index1); // What?
    b = &map2.at(index2);
} catch(std::out_of_range&) {
    return false;
}
std::cout << (*a)[(*b)[42]];
return true;
Run Code Online (Sandbox Code Playgroud)

有没有更好的办法?像Python中的 try-except-else之类的东西会很好,但是在C++中不存在.

c++ stdmap try-catch

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

是否初始化了std :: map中原始类型的值?

请考虑以下代码:

map<int,int> m;
for(int i=0;i<10000;++i) m[i]++;
for(int i=0;i<10000;++i) printf("%d",m[i]);
Run Code Online (Sandbox Code Playgroud)

我认为打印出的值是未定义的,因为原始类型没有默认构造函数,但是每次测试时我都得到10000个1.

为什么要初始化?

c++ stdmap primitive-types

11
推荐指数
2
解决办法
879
查看次数

如何使用Swig将JavaScript中的关联数组映射到C++中的字符串映射?

颇为相似,这个问题我想包装与痛饮函数,该函数mapstrings到stringS:

void foo(std::map<std::string, std::string> const& args);
Run Code Online (Sandbox Code Playgroud)

对于Python,它足以为地图创建别名:

namespace std {
    %template(map_string_string) map<string, string>;
}
Run Code Online (Sandbox Code Playgroud)

代码生成器将创建包装函数map_string_string,甚至自动使用它.

my_module.foo({'a': 'b', 'c', 'd'})
Run Code Online (Sandbox Code Playgroud)

将被正确调用,不符合签名的值将被忽略.

我如何为JavaScript执行此操作?

我尝试了相同的(当然)和包装器生成但是当我尝试这样调用时foo:

my_module.foo({'a':'b', 'c':'d'});
Run Code Online (Sandbox Code Playgroud)

我明白了

/path/to/example.js:3
my_module.foo({'a':'b', 'c':'d'});
          ^

Error: in method 'foo', argument 1 of type 'std::map< std::string,std::string > const &'
    at Object.<anonymous> (/path/to/example.js:8:7)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3 …
Run Code Online (Sandbox Code Playgroud)

javascript c++ swig stdmap

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

如何在std :: map中找到指定范围内的元素?

是否有相同的版本std::find(first, last)但是std::map?即,是否有一个版本std::mapfind方法可以搜索a中的元素map,但是只将搜索限制在指定的[first, last)范围内?理想情况下,解决方案应该是对数的大小[first, last).

我所看到的,std::map::find它本身不支持这个功能(它总是搜索整个地图).

c++ algorithm search stdmap

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