小编Rya*_*yan的帖子

Lambda的显式返回类型

当我尝试编译此代码(VS2010)时,我收到以下错误: error C3499: a lambda that has been specified to have a void return type cannot return a value

void DataFile::removeComments()
{
  string::const_iterator start, end;
  boost::regex expression("^\\s?#");
  boost::match_results<std::string::const_iterator> what;
  boost::match_flag_type flags = boost::match_default;
  // Look for lines that either start with a hash (#)
  // or have nothing but white-space preceeding the hash symbol
  remove_if(rawLines.begin(), rawLines.end(), [&expression, &start, &end, &what, &flags](const string& line)
  {
    start = line.begin();
    end = line.end();
    bool temp = boost::regex_search(start, end, what, expression, flags);
    return temp; …
Run Code Online (Sandbox Code Playgroud)

c++ lambda visual-c++ c++11 visual-c++-2012

74
推荐指数
3
解决办法
9万
查看次数

两个belongs_to关联的相同模型

我有一个模型,PointOfContact其中has_many Systems.从Systems侧面我想要识别PointOfContacttechnical_managerproject_manager(或两者).虽然仍然只PointOfContact在DB中保留1次.

我的尝试如下:

class System < ActiveRecord::Base
  belongs_to :project_manager, :class_name => 'PointOfContact'
  belongs_to :technical_manager, :class_name => 'PointOfContact'
end

class PointOfContact < ActiveRecord::Base
  has_many :systems
end
Run Code Online (Sandbox Code Playgroud)

当我运行我的规范(示例如下)时,我可以正确地创建System联系点关联.但是,PointOfContact它并不知道它与System的关联.这是为什么?

@sys = System.create
@tm = PointOfContact.create
@pm = PointOfContact.create

@sys.project_manager = @pm
@sys.technical_manager = @tm

@pm.systems.should have(1).items #> expected 1 items, got 0
Run Code Online (Sandbox Code Playgroud)

associations model-associations ruby-on-rails-3

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

将stdout和stderr重定向到Function

我需要帮助将输出(stdin和stdout)从系统命令发送到bash函数,同时仍然接受来自参数的输入.类似下面的例子.有人能指出我正确的道路吗?

LogMsg()
{
  DateTime=`date "+%Y/%m/%d %H:%M:%S"`
  echo '*****'$DateTime' ('$QMAKESPEC'): '$1 >> "$LogFile"
  echo $DateTime' ('$QMAKESPEC'): '$1
}

# Already works
LogMsg "This statement is sent directly"

# Wish I could do this:
# Capture both stdout & stderr of a system function to the logfile
# I do not presume that any of the syntax that follows is good
make 2>&1 >(LogMsg)
Run Code Online (Sandbox Code Playgroud)

bash

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

Rails迁移创建表主键

我正在编写一个迁移脚本来创建一个表,其中包含一个名为的主键列,guid并且是一个VARCHAR(25).问题是我觉得我必须加倍努力才能一步到位才能实现目标.

如果我跑:

create_table(:global_feeds, :primary_key => 'guid') do |t|
  t.string :guid, :limit => 25
  t.text :title
  t.text :subtitle

  ...

  t.timestamps
end
Run Code Online (Sandbox Code Playgroud)

我得到一个表,其中一个名为guidno column 的主键被调用id(这就是我想要的).但是,问题是guid列是INT(11)打开自动增量的.所以我必须运行一个额外的命令:

change_column :global_feeds, :guid, :string, :limit => 25
Run Code Online (Sandbox Code Playgroud)

似乎有点费解,基本上必须运行两个SQL命令才能获得我认为应该可以在一个中执行的操作.

关于如何优化这个的任何建议?

mysql migration optimization ruby-on-rails-3

15
推荐指数
2
解决办法
3万
查看次数

偏导数

我正在尝试编写一个能够执行N维混合偏导数的算法.我知道我需要能够实现什么,但我似乎无法想出实现N维情况所需的正确循环/递归.

以下是前4个维度的模式:

| 1D  wzyx  | 2D           | 3D           | 4D           |
----------------------------------------------------------
| dx (0001) | dx    (0001) | dx    (0001) | dx    (0001) |
|           | dy    (0010) | dy    (0010) | dy    (0010) |
|           | dyx   (0011) | dyx   (0011) | dyx   (0011) |
|           |              | dz    (0100) | dz    (0100) |
|           |              | dzx   (0101) | dzx   (0101) |
|           |              | dzy   (0110) | dzy   (0110) |
|           |              | …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm math

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

将Objective-C(#define)宏转换为Swift

简单地说,我试图将#define宏转换为某种原生的Swift数据结构.只是不确定如何或什么样的.

细节

我想尝试将以下内容#define从Objective-C 复制到Swift.资料来源:JoeKun/FileMD5Hash

#define FileHashComputationContextInitialize(context, hashAlgorithmName)                    \
    CC_##hashAlgorithmName##_CTX hashObjectFor##hashAlgorithmName;                          \
    context.initFunction      = (FileHashInitFunction)&CC_##hashAlgorithmName##_Init;       \
    context.updateFunction    = (FileHashUpdateFunction)&CC_##hashAlgorithmName##_Update;   \
    context.finalFunction     = (FileHashFinalFunction)&CC_##hashAlgorithmName##_Final;     \
    context.digestLength      = CC_##hashAlgorithmName##_DIGEST_LENGTH;                     \
    context.hashObjectPointer = (uint8_t **)&hashObjectFor##hashAlgorithmName
Run Code Online (Sandbox Code Playgroud)

显然#define在Swift中不存在; 因此我不是在寻找1:1的端口.更普遍的只是它的精神.

首先,我做了一个enum电话CryptoAlgorithm.为了这个问题,我只关心支持两种加密算法; 但是没有什么能阻止我进一步扩展它.

enum CryptoAlgorithm {
  case MD5, SHA1
}
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.现在实施了digestLength.

enum CryptoAlgorithm {
  case MD5, SHA1

  var digestLength: Int {
    switch self {
    case .MD5:
      return Int(CC_MD5_DIGEST_LENGTH)
    case .SHA1:
      return Int(CC_SHA1_DIGEST_LENGTH)
  }
}
Run Code Online (Sandbox Code Playgroud)

再次,到目前为止一切顺利.现在实施了 …

generics enums struct swift swift2

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

将@State转换为发布者

我想@State在UI和计算值中都使用变量。

例如,假设我有一个TextField约束@State var userInputURL: String = "https://"。我将如何处理并将其userInputURL连接到发布商,以便将map其集成到URL

伪代码:

$userInputURL.publisher()
      .compactMap({ URL(string: $0) })
      .flatMap({ URLSession(configuration: .ephemeral).dataTaskPublisher(for: $0).assertNoFailure() })
      .eraseToAnyPublisher()
Run Code Online (Sandbox Code Playgroud)

swift swiftui combine

8
推荐指数
1
解决办法
337
查看次数

从C#访问XAML实例化对象

在我的XAML中,我声明了一个名为DataConnection的类的实例,该实例名为MyConnection.

<Window.Resources>
        <!-- Create an instance of the DataConnection class called MyConnection -->
        <!-- The TimeTracker bit comes from the xmlns above -->
        <TimeTracker:DataConnection x:Key="MyConnection" />
        <!-- Define the method which is invoked to obtain our data -->
        <ObjectDataProvider x:Key="Time" ObjectInstance="{StaticResource ResourceKey=MyConnection}" MethodName="GetTimes" />
        <ObjectDataProvider x:Key="Clients" ObjectInstance="{StaticResource ResourceKey=MyConnection}" MethodName="GetClients" />
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

XAML部分中的所有内容都可以正常工作.我想要的是能够从我的C#代码引用我的MyConnection实例.

怎么可能?

c# oop wpf xaml

5
推荐指数
2
解决办法
2054
查看次数

数据库优先验证

我有一个自动生成的实体框架模型.它是使用数据库第一种方法生成的.该mid_initial列具有数据库定义的约束,该约束将列限制为最多3个字符的长度.

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Agency.DataAccess.RegistrationModel
{
    using System;
    using System.Collections.Generic;

    public partial class Registrant
    {
        public Registrant()
        {
        }

        public int id { get; set; }
        public string fname { get; set; }
        public string mid_initial { get; …
Run Code Online (Sandbox Code Playgroud)

entity-framework ef-database-first

5
推荐指数
1
解决办法
3092
查看次数

ActiveRecord子查询内部连接

我试图将"原始"PostGIS SQL查询转换为Rails ActiveRecord查询.我的目标是将两个连续的ActiveRecord查询(每个需要大约1毫秒)转换为单个ActiveRecord查询(~1ms).使用下面的SQL ActiveRecord::Base.connection.execute我能够验证时间的减少.

因此,我的直接请求是帮助我将此查询转换为ActiveRecord查询(以及执行它的最佳方法).

SELECT COUNT(*)
FROM "users"
INNER JOIN (
  SELECT "centroid"
  FROM "zip_caches"
  WHERE "zip_caches"."postalcode" = '<postalcode>'
) AS "sub" ON ST_Intersects("users"."vendor_coverage", "sub"."centroid")
WHERE "users"."active" = 1;
Run Code Online (Sandbox Code Playgroud)

请注意,该值<postalcode>是此查询中唯一的可变数据.显然,这里有两个型号UserZipCache.User与...没有直接关系ZipCache.

当前的两步ActiveRecord查询如下所示.

zip = ZipCache.select(:centroid).where(postalcode: '<postalcode>').limit(1).first
User.where{st_intersects(vendor_coverage, zip.centroid)}.count
Run Code Online (Sandbox Code Playgroud)

ruby activerecord postgis arel rgeo

5
推荐指数
1
解决办法
8372
查看次数