标签: find

c ++ map find找不到特定的Key

我正在使用地图来存储一堆键和值。我想使用 find() 来查找键并返回值。不幸的是,当我找不到钥匙时,它会很沮丧。如果找不到密钥,如何使其返回 0?

 int bag::getItem( const string item)
 {
    return this->bagItems.find(item)->second;
    return 0;
 }
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激。

c++ map find

0
推荐指数
1
解决办法
4515
查看次数

获取类等于 active 的 div 索引

我有一个 twitter bootstrap carousel:

<div id="myCarousel" class="carousel slide">
    <div class="carousel-inner">
       <div class="item">
            <div id="container"></div>
       </div>
       <div class="item active">
            <div id="container2"></div>
       </div>
       <div class="item">
            <div id="container3"></div>
       </div>
    </div>
    <!-- Controls -->
    <a class="left carousel-control" href="#myCarousel" data-slide="prev">«</a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next">»</a>
</div>
Run Code Online (Sandbox Code Playgroud)

现在,当我点击时,例如。一个按钮。我想要带有 class = 'active' 的项目的索引。在此示例中,它将是第二个 div,因此 index = 1(从 0 开始)。

要使用 class = "active" 获取 div,我这样做:

var activediv = $('.item').find('.active');
Run Code Online (Sandbox Code Playgroud)

但是当我想像这样找到元素的索引时:

var index = activediv.index();
Run Code Online (Sandbox Code Playgroud)

我总是在我的控制台中得到“-1”,所以他找不到该项目......但为什么呢?

有谁知道我做错了什么?

javascript indexing jquery find twitter-bootstrap

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

从列表中查找文件并将它们复制到新目录中

我想通过 OS X 的命令行终端查找文件,然后将找到的文件复制到新目录。我要查找的文件的文件名在 listexample.txt 文件中,其中大约有 5000 个文件名。

文件 listexample.txt 如下所示:

1111 00001 55553.bmp
1113 11312 24125.bmp
…
Run Code Online (Sandbox Code Playgroud)

我试过这样的事情:

find /directory -type f "`cat listexample.txt`" -exec cp {} …
Run Code Online (Sandbox Code Playgroud)

但无法让它运行。

我现在有这个,但它不起作用:

cat listexample.txt | while read line; do grep "$line" listexample.txt -exec find /directorya "$line" -exec cp {} /directoryb \; done
Run Code Online (Sandbox Code Playgroud)

这个想法是读取列表 example.txt 的行,然后使用 grep 获取该行,在目录 a 中找到文件,然后将找到的文件复制到新目录 b。我认为由于我的文件名的性质,见上文,名称中也存在空格问题。

我也开始使用这种方法来查看发生了什么,但并没有走多远。

for line in `cat listexample.txt`; do grep $line -exec echo "Processing $line"; done
Run Code Online (Sandbox Code Playgroud)

macos terminal find

0
推荐指数
1
解决办法
3322
查看次数

找到最接近给定值的索引

我有一个有序数组:

btnDrag.pos = [0, 65, 131, 196, 259, 323, 388, 453, 517];
Run Code Online (Sandbox Code Playgroud)

还有一个在拖动停止时触发的函数:

btnDrag.draggable({
    axis: 'x',
    containment: 'parent',
    stop: function() {
        var index = (function(){
            var new_x = btnDrag.position().left;
            // now, how to find the closest index in btnDrag.pos relative to new_x ?
            // return index;
        })();
        btnDrag.animate({
            'left': (btnDrag.pos[index] + 'px')
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

数组值是允许 btnDrag 停留的点(在轴“x”中)。

因此,该函数必须返回值与 btnDrag go 最接近的索引。

提前致谢。

javascript algorithm find

0
推荐指数
1
解决办法
1434
查看次数

使用 find 命令格式化时间戳

此命令用于-printf '%Tc %p\n'提供上次修改文件的日期。

$ find ./ -daystart -mtime -3 -type f  ! -mtime -1  -printf '%Tc %p\n'
Tue 08 Mar 2016 12:25:01 NZDT ./compareKPIs-log
Tue 08 Mar 2016 18:04:58 NZDT ./backup_public_html_20160308.tgz
Tue 08 Mar 2016 18:04:58 NZDT ./log-file
Tue 08 Mar 2016 12:25:01 NZDT ./compareKPIs-error
Mon 07 Mar 2016 18:05:02 NZDT ./backup_public_html_20160307.tgz
Run Code Online (Sandbox Code Playgroud)

我想要做的是将日期格式控制为YYYY-mm-dd. 我该怎么做呢?(如果需要,也可以在此处添加 hh:mm:ss)

例如-printf '%TY %p\n'会给我年份%TY

$ find ./ -daystart -mtime -3 -type f  ! -mtime -1  -printf '%TY %p\n' …
Run Code Online (Sandbox Code Playgroud)

linux printf date find

0
推荐指数
1
解决办法
1651
查看次数

实体框架按主键过滤

我正在编写一个通用的 crud 服务,我正在尝试Get使用可选的虚拟方法来实现该方法以包含属性但是我遇到了一些麻烦,因为FindAsync仅在以下位置声明DbSet

public async virtual Task<TDTO> Get(object[] id)
{
     // I want to do something like this
     var entity = await this.ApplyGetIncludes(this.GetEntityDBSet()).FindAsync(id)
     return this.AdaptToDTO(entity);
}

protected virtual DbSet<TEntity> GetEntityDBSet()
{
    return this._context.Set<TEntity>();
}

protected virtual IQueryable<TEntity> ApplyGetIncludes(IQueryable<TEntity> queryable)
{
    return queryable;
}
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情,如上所示:

var entity = await this.ApplyGetIncludes(this.GetEntityDBSet()).FindAsync(id)
Run Code Online (Sandbox Code Playgroud)

但我知道这行不通,因为我们需要数据库集,所以我会设置做这样的事情:

var entity = await this.ApplyGetIncludes(this.GetEntityDBSet().FilterByPK(id))
                      .FirstOrDefaultAsync();
Run Code Online (Sandbox Code Playgroud)

有谁知道我如何从 a 中按主键过滤DbSet

c# entity-framework find

0
推荐指数
1
解决办法
1020
查看次数

使用第一列中的值查找矩阵中的行

我有矩阵 A

A=[ 1 2 3;
    3 4 5;
    1 6 7; 
    2 2 1 ]
Run Code Online (Sandbox Code Playgroud)

我需要矩阵,B其中的行在A第一列中有1个:

B=[ 1 2 3;
    1 6 7 ]
Run Code Online (Sandbox Code Playgroud)

matlab rows matrix find

0
推荐指数
1
解决办法
44
查看次数

使用GNU find递归创建具有随机内容的文件

我的目的是快速创建如下所示的目录树:

.
|-- dir1
|   |-- 01
|   |   `-- file_00
|   |       
|   |-- 02
|   |   `-- file_00
|   |       
|   |-- 03
|   |   `-- file_00
|   |       
|   |-- 04
|       `-- file_00
|           
`-- dir2
    |-- 01
    |   `-- file_00
    |       
    |-- 02
    |   `-- file_00
    |       
    |-- 03
    |   `-- file_00
    |      
    `-- 04
        `-- file_00
Run Code Online (Sandbox Code Playgroud)

我完成的是:

mkdir {dir1,dir2}
mkdir ./{dir1,dir2}/{01,02,03,04}
touch ./{dir1,dir2}/{01,02,03,04}/file_00
Run Code Online (Sandbox Code Playgroud)

但是当我想追加一个随机数时,$RANDOM随机就足够了,每次file_00我遇到问题.我试过了:

find . -type f -name 'file_00' …
Run Code Online (Sandbox Code Playgroud)

bash find windows-subsystem-for-linux

0
推荐指数
1
解决办法
44
查看次数

bash中由globbing,变量替换和find命令组合而混淆

我试图传递一个引用的globbing模式来使用变量在脚本中查找命令.没有eval就行不通.这是没有eval的方法吗?

首先,我使用followin命令:

find . -name '*a*'
Run Code Online (Sandbox Code Playgroud)

它工作正常并产生以下输出:

./AaA
./dir1/aaa.tst
./dir1/zabc1122333.tst
./dir2/dir3/zabc1122333.tst
./yyy/AaA
./zabc1122333.tst
Run Code Online (Sandbox Code Playgroud)

现在我想用一个变量来代替引用的glob模式' a '.

这不起作用:

A='*a*' ; find . -name $A
Run Code Online (Sandbox Code Playgroud)

它产生:

find: zabc1122333.tst: unknown primary or operator
Run Code Online (Sandbox Code Playgroud)

以下四个命令也不起作用.他们什么都没有:

A="'*a*'" ; find . -name $A

A=\'\*a\*\' ; find . -name $A

A=\'*a*\' ; find . -name $A

A='\*a\*' ; find . -name $A
Run Code Online (Sandbox Code Playgroud)

最后它适用于eval:

A=\'\*a\*\' ; eval find . -name $A

./AaA
./dir1/aaa.tst
./dir1/zabc1122333.tst
./dir2/dir3/zabc1122333.tst
./yyy/AaA
./zabc1122333.tst
Run Code Online (Sandbox Code Playgroud)

没有评估可以做到吗?

bash find sh quoting string-substitution

0
推荐指数
1
解决办法
42
查看次数

字符串replace()无法按预期工作

replace()在python中遇到了一些麻烦。这是我的代码:

string = input()
word = string.find('word')
if word >= 1:
    string = string.replace('word', 'word.2')
print(string)
Run Code Online (Sandbox Code Playgroud)

输出结果为word。不是word.2吗?我糊涂了。有什么帮助吗?

编辑:经过一段时间解决问题后,我发现问题现在是“为什么string.find('word')输入等于0 word

python replace find

0
推荐指数
1
解决办法
110
查看次数