我有一个Map,它的键包含字符串,并为其值设置(包含整数)
说我的钥匙看起来像这个"苹果","香蕉","橙色"等.
用户输入文本,我将其保存为String变量.如何在地图中搜索相同的密钥?因此,如果用户键入"apple",我如何将该String赋予方法并让方法在地图中搜索"apple"键并返回与其关联的整数集(值)?
谢谢
在下面的代码中,而不是将输出视为
0 -> 1 2
1 -> 2 3
..
..
99 ->100 101
Run Code Online (Sandbox Code Playgroud)
我得到输出,
0 -> 100 101
1 -> 100 101
...
99 -> 100 101
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题,究竟哪里出错了?我发现调试时,在第一次迭代中它存储
0 -> 1 2
Run Code Online (Sandbox Code Playgroud)
它更新的第二次迭代,如,
0 -> 2 3
1 -> 2 3
Run Code Online (Sandbox Code Playgroud)
为什么?
class abc{
public:
int x, y;
};
std::map<int, abc*> MAP;
int main()
{
abc *ab;
ab = new abc();
int i = 0;
for(i = 0; i < 100; i++)
{
ab->x = i + 1;
ab->y …Run Code Online (Sandbox Code Playgroud) 问题实际上是标题所说的.
的原因的问题:
在mapFUNC比较慢for在下面的代码循环.
是因为我的代码中存在某些问题,还是还有其他问题?
码:
import timeit
setup = '''
def translate(x):
x[1]+=1
x[2]+=1
x[3]+=1
atoms = [[1,1,1,1]]*1000
'''
smt1 = '''for i in atoms: translate(i)'''
smt2 = '''map(translate, atoms)'''
time_for = timeit.Timer(setup=setup, stmt=smt1)
time_map = timeit.Timer(setup=setup, stmt=smt2)
print time_for.timeit(10000)
print time_map.timeit(10000)
Run Code Online (Sandbox Code Playgroud)
输出(Windows 7(64位)I-3 2nd gen):
>>>
3.4691164256
3.5064888507
Run Code Online (Sandbox Code Playgroud)
输出(Windows 7(32位)core2duo):
>>>
5.58571625252
6.25803459664
Run Code Online (Sandbox Code Playgroud)
我想我应该提到我使用的是Python 2.7.3,所以虽然map在Python 3中是一个生成器,但在Python 2中并非如此,所以这个"问题"无法在Python 3上复制.
更新:
为了解决那些说原子应该是不可变的人,这里有一个不同版本的设置(它更慢,但仍然显示差异):
setup = '''
def translate(x):
return tuple(i+1 for i in x)
atoms = …Run Code Online (Sandbox Code Playgroud) 我有这个编码功能:
class Encode a where
encode :: a -> [Bit]
Run Code Online (Sandbox Code Playgroud)
我在编写一个函数时遇到问题,该函数将类型a的列表编码为位列表.我想递归编码列表的元素.根据我的理解,您可以使用map函数来实现此目的.问题是encode返回一个列表[Bit],而map只需要Bit.我怎么解决这个问题?这是该计划的相关部分.
instance Encode a => Encode [a] where
encode [] = [I, O, O, I, O, I]
encode m = ([I, O, O] ++ (map encode m) ++ [I, O, I])
Run Code Online (Sandbox Code Playgroud) 我有一个MKMapRect.
我如何CLLocationCoordinate在里面创建一个随机的?
我知道有arc4random(),但我怎样才能将它用于GPS坐标?
我有2个char数组,如"const char*arr1 [ArrSize] = {"Blah1","Wibble1","Shrug1"};".为了将它们放入向量中,我发现了一个很好的快速解决方案
void fillVecTest()
{
const int ArrSize = 3;
const char *arr1[ArrSize] = {"Blah1", "Wibble1", "Shrug1"};
const char *arr2[ArrSize] = {"Blah2", "Wibble2", "Shrug2"};
std::vector<std::string> vec1(arr1, arr1+ArrSize);
std::vector<std::string> vec2(arr2, arr2+ArrSize);
std::vector<std::string>::iterator l_It1Vec1;
std::vector<std::string>::iterator l_It = vec1.end();
l_It = find(vec1.begin(), vec1.end(), std::string("Blah1"));
if(l_It != vec1.end())
{
size_t l_pos = l_It - vec1.begin();
printf("found %s, pos=%u val: %s\n", l_It->c_str(),l_pos, vec2[l_pos].c_str());
}
}
Run Code Online (Sandbox Code Playgroud)
现在我认为也应该可以将两者直接放入地图中,因为arr1是关键,而arr2是值.我尝试了一些方法,但我没有成功.
void fillMapTest()
{
const int ArrSize = 3;
const char *arr1[ArrSize] = {"Blah1", "Wibble1", "Shrug1"};
const …Run Code Online (Sandbox Code Playgroud) 据我所知,以下方案
#include <map>
int main()
{
std::map<int,int> foo;
std::map<int,int>::iterator start = foo.begin();
while (start++ != foo.end())
;
}
Run Code Online (Sandbox Code Playgroud)
应该终止,但它使用libstdc ++ 4.7.2无限循环.此程序显示的行为是否正确,或者标准库中是否存在错误?在迭代器上运算符++(int)的操作属性是什么?
是否可以创建一个密钥是Collection(任何类型的集合)的映射?
如果我在大多数常见的收藏品上尝试它,我被告知收藏品不能用于比较.
我一直在尝试为自定义集合编写compareTo函数,但我很挣扎.
要么我需要编写compareTo,要么我需要找到一个接受地图接受的集合/集合的预制地图.
如何在地图上将集合用作关键字?我已经查看了Stack溢出,我已经多次搜索过这个问题,但我从来没有找到一个可靠的解决方案!
我想这样做的原因是我在Java中编写了一个模拟卡改组的'shuffle'模拟.我希望能够计算特定手(建模为集合)出现的次数.它看起来像这样:
H4,C3,D2: 8
H9,D6,S11: 10
......
Run Code Online (Sandbox Code Playgroud) 英文解释:我要访问一个名为成员foo从关键元素"bar"从一个std::map<string, Parent *>地方Parent是一个抽象基类.
码:
#include <map>
#include <string>
#include <iostream>
using namespace std;
class Parent {
public:
virtual ~Parent() {}
};
class Child: public Parent {
public:
Child(): var(2) {}
int var;
};
int main() {
map<string, Parent *> children;
children["bar"] = new Child;
cout << children["bar"]->var << endl; // Erroneous line
cout << children.find("bar")->second->var << endl; // Different method, still gives the same error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误: ‘class Parent’ …
我有以下方法C++,只tableId从地图中删除与特定关联的元素.
69 void
70 ObjectFinder::flush(uint64_t tableId) {
71
72 RAMCLOUD_TEST_LOG("flushing object map");
74 // find everything between tableId, 0
75 // keep scanning util find all the entries for that table
76 std::map<TabletKey, ProtoBuf::Tablets::Tablet>::const_iterator it;
79 for (it = tableMap.begin(); it != tableMap.end(); it++) {
80 TabletKey current = it->first;
81 if (tableId == current.first) {
82 tableMap.erase(current);
83 }
84 }
85 std::cout << "hello" << std::endl;
87 }
Run Code Online (Sandbox Code Playgroud)
进入代码时gdb我发现在循环迭代后发生了无限for循环.该行85 …
map ×10
c++ ×5
iterator ×2
java ×2
stl ×2
char ×1
collections ×1
comparable ×1
haskell ×1
ios ×1
list ×1
objective-c ×1
polymorphism ×1
python ×1
random ×1
search ×1