小编Aki*_*ira的帖子

使用c ++的余弦查找表

这是一个片段,应该生成2048个元素的余弦查找表,取自Changyi Gu的Building Embedded Systems一书:

#include <cmath>
#include <array>

template<typename T>
constexpr T look_up_table_elem (int i) {
    return {};
}

template<>
constexpr uint16_t look_up_table_elem (int i) {
    return round (cos (static_cast <long double>(i) / 2048 * 3.14159 / 4) * 32767);
}


template<typename T, int... N>
struct lookup_table_expand{};

template<typename T, int... N>
struct lookup_table_expand<T, 1, N...> {
    static constexpr std::array<T, sizeof...(N) + 1> values = {{ look_up_table_elem<T>(0), N... }};
};

template<typename T, int L, int... N> 
struct lookup_table_expand<T, L, N...>: lookup_table_expand<T, …
Run Code Online (Sandbox Code Playgroud)

c++ templates lookup-tables c++11

10
推荐指数
1
解决办法
1230
查看次数

基于constexpr的模式创建位掩码

我想实现一个模板函数,它在编译时为整数类型生成位掩码.这些掩模应基于8位模式,其中模式将连续重复以填充整数.以下示例正是我想要的,但在运行时:

#include <iostream>
#include <type_traits>
#include <cstring>

template<typename Int>
typename std::enable_if<std::is_integral<Int>::value, Int>::type
make_mask(unsigned char pattern) {
    Int output {};
    std::memset(&output, pattern, sizeof(Int));
    return output;
}

int main() {
    auto mask = make_mask<unsigned long>(0xf0);
    std::cout << "Bitmask: '" << std::hex << mask << "'" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

上面代码的输出是:

Bitmask: 'f0f0f0f0f0f0f0f0'
Run Code Online (Sandbox Code Playgroud)

我知道优化器可以消除上面代码中的整个函数调用,但我正在寻找一个constexpr使用的解决方案.

c++ bit-manipulation constexpr c++11 c++14

8
推荐指数
2
解决办法
639
查看次数

将十六进制字符串转换为QByteArray

我需要将QString已经为十六进制格式的转换为QByteArray。例如:

QString a = "AF5603B4"
Run Code Online (Sandbox Code Playgroud)

应存储QByteArray为:

QByteArray ba[4] = { 0xAF, 0x56, 0x03, 0xB4 }
Run Code Online (Sandbox Code Playgroud)

如何在Qt 5.9中做到这一点?我尝试使用许多方法,但是所有这些方法都会将字符串字符转换为它们的ASCII值,然后给出该十六进制值。

我找到Convert.toByte了在C#中使用的方法;我可以使用Qt中的等效项吗?

c++ string qt qbytearray

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

展平包含单个项目的列表列表

我有一个由最多包含单个字符串的列表组成的列表,例如:

var fruits = new List<List<string>>
{
    new List<string>(),
    new List<string> { "Apple" },
    null,
    new List<string> { "Banana" },
};
Run Code Online (Sandbox Code Playgroud)

我想从上面的列表中提取字符串,不允许包含字符串的列表有多个项目。也就是说,我需要得到以下结果:

{ "Apple", "Banana" }
Run Code Online (Sandbox Code Playgroud)

到目前为止,我一直在尝试以下操作:

var result = fruits
    .Where(x => x != null)
    .Select(x => x.SingleOrDefault())
    .Where(x => x != null);
Run Code Online (Sandbox Code Playgroud)

有没有更简单的解决方案来做到这一点?

c# linq

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

在C中的while循环中使用指针发布增量

我正在写strcat函数

/*appends source string to destination string*/

#include <stdio.h>

int main()
{
    char srcstr[100], deststr[100];
    char *psrcstr, *pdeststr;

    printf("\n Enter source string: ");
    gets(srcstr);
    printf("\n Enter destination string: ");
    gets(deststr);

    pdeststr = deststr;
    psrcstr = srcstr;

    while(*pdeststr++)
        ;
    while(*pdeststr++ = *psrcstr++)
        ;

    printf("%s", deststr);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

对于srcstr = " world"deststr = "hello"我得到的hello,我希望看到hello world,这是我所看到的,如果我第一次改变while这样

while(*pdeststr);
    pdeststr++;
Run Code Online (Sandbox Code Playgroud)

为什么我不能在第一行中写所有内容while,就像在第二行一样while

c c-strings

4
推荐指数
1
解决办法
1841
查看次数

C中的决策 - ?:运算符示例

我知道运算符是如何工作的,但我不明白下面代码中的第二种情况如何给出1作为结果.

#define MIN(a,b) a<b ? a:b  
int x = MIN(1,2);  //case 1, output is 1
int x = MIN(1,1+1); // case 2
Run Code Online (Sandbox Code Playgroud)

编译器不应该看到这个(情况2)为"是1 <1 + 1?",我认为应该是"1 <1?不是",表达式3被执行并得到:"b"= 1 +1 = 2.这是因为上面的宏中a和b周围没有括号,或者可能是因为我遵循自己的规则而感到迷茫?请帮忙....

另外,你如何解释这个案例:

int x = MIN(1,2) + 1;  //should we consider the "1" on the right?
Run Code Online (Sandbox Code Playgroud)

c ternary-operator c-preprocessor

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

将 next(), hasNext() 迭代器接口变成 begin(), end() 接口

我必须使用我无法更改的外部库。该库以及其他库可以通过其内部逻辑标记特殊格式的文件。令牌生成器提供了一个用于访问令牌的迭代器接口,类似于以下简化示例:

class Tokenizer {
public:
    /* ... */
    Token token() const; // returns the current token
    Token next() const; // returns the next token
    bool hasNext() const; // returns 'true' if there are more tokens
    /* ... */
};
Run Code Online (Sandbox Code Playgroud)

我想执行一个迭代包装器所呈现的Tokenizer它允许使用标准的算法库std::copy_ifstd::count等)。更具体地说,如果迭代器包装器满足输入迭代器的要求就足够了。

我目前的试验如下所示:

class TokenIterator {
public:
    using iterator_category = std::input_iterator_tag;
    using value_type = Token;
    using difference_type = std::ptrdiff_t;
    using pointer = const value_type*;
    using reference = const value_type&;

    explicit TokenIterator(Tokenizer& tokenizer) …
Run Code Online (Sandbox Code Playgroud)

c++ iterator c++14

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

if语句在内部,而依赖于依赖的同一个变量

考虑以下计划.我想知道为什么这些代码以不同的方式表现.提前提前.

这不打印任何

#include <stdio.h>

int main() {
    int i = 0;

    while(i < 10) {
        if(i < 7)
            printf("value is%d", i++); 
    }
}
Run Code Online (Sandbox Code Playgroud)

虽然这样做

#include <stdio.h>
int main() {
    int i = 0;

    while(i < 10) {
        if(i < 7)
            printf("value is%d\n", i++);
    }
}
Run Code Online (Sandbox Code Playgroud)

c conditional loops if-statement while-loop

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

关于 std::ostream 构造函数

我想这样使用std::ostream

int main()
{
    std::ostream os;
    os << "something ..." << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有一个错误说 ostream 构造函数受到保护:

错误:'std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits]' 受保护。

但我记得operator<<可以像这样超载:

// In a class. 
friend std::ostream & operator<<(std::ostream& out, const String & s) {
    out << s.m_s;
    return out;
}
Run Code Online (Sandbox Code Playgroud)

关于为什么我的代码不起作用的任何建议?

c++ ostream

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