我有一个std::vector<std::string>,它有43,000个字典单词。我大概有315000个单词,每一个单词我都需要确定它是否是一个有效的单词。这需要几秒钟,我需要尽快完成任务。
有什么最好的办法来完成这件事吗?目前,我重复了每一次尝试:
for (std::string word : words) {
if (!(std::find(dictionary.begin(), dictionary.end(), word) != dictionary.end())) {
// The word is not the dictionary
我尝试了使用std::algorithms,但失败了:我有一个std::map<key_t,value_t> cache和一个std::set<key_t> selected_items,我想从cache中删除键/值对,但selected_items中包含的键除外。
下面是我写的没有算法的东西:
//This could really be written better with std::algorithms but time...
//Delete old
for (auto pair = cache.begin(); pair != cache.end(); )
我希望比较两个集合,并通过迭代第一个集合来显示第二个集合中缺失的元素。
我已经用过列表了,但是它看起来像是在无序列表上迭代来找到元素的开销。
#include <iostream>
#include <list>
using std::list;
bool isExist(list <int> &original, int i)
{
list <int>::iterator iter;
for (iter = original.begin(); iter != original.end(); iter++)
{
if (*i
我有两个数组,每个数组都有5组随机数。
首先,我显示第一个数组中所有数字的列表;然后,我需要将不在第一个数组中的任何数字添加到该列表中,并显示每个数字。为此,我使用另一个数组将唯一值放入以进行显示。我已经有了一个显示当前数组的函数。
下面是我遇到问题的代码:
//SIZE is defined in the beginning as 5.
printArray(array1);
int i, j;
//For each number in array1, compare each number in array2 to it.
for(j=0; j<SIZE; j++)
{
当我在一个erase()容器中使用class vector方法时,我会得到分段错误。
我在比较两个向量,所以我想从其中之一移除另一个向量中不存在的元素。为此,我使用迭代器和erase(),如下所示:
#include <vector>
int main () {
std::vector<int> vector1 {6,7,5,44,3,10,9,17,1};
std::vector<int> vector2 {1,2,3,5,8};
for (std::vector<int>::iterator it (vector2.begin());
我有很少的地图std::map< char, int >
First map:
a - 1
b - 2
c - 3
Second map:
a - 5
c - 6
e - 7
我想将它们连接到std::map< char, std::vector< int > >中
a - 1 5
b - 2 0
c - 3 6
e - 0 7
做这件事最好的方法是什么?
我有一组(大的)整数S,我想运行以下伪代码:
set result = {};
while(S isn't empty)
{
int i = S.getArbitraryElement();
result.insert(i);
set T = elementsToDelete(i);
S = S \ T; // set difference
}
函数elementsToDelete是有效的(在S的初始大小中是次线性的),并且T的大小很小(假设它是常数)。T可能不再包含S中的整数。
是否有一种比O更快的方法来实现上面的内容?我怀疑我应该能够得到O(x,s,s,x,k),其