Questions in category: C++ (C++)
软件 >> C++
<[1] [2] [3] [4] >

1. [C++] find_last_of

Posted by haifeng on 2024-04-08 09:58:20 last update 2024-04-08 10:03:20 | Answers (0) | 收藏


C++ 中的 find_last_of() 函数经常会被误认为在字符串中搜寻某个字串最后出现的位置. 实际上它的功能是在字符串中搜索与其参数中指定的任何字符匹配的最后一个字符.

find_last_of() 有以下多种声明.

string (1)
size_t find_last_of (const string& str, size_t pos = npos) const;
c-string (2)
size_t find_last_of (const char* s, size_t pos = npos) const;
buffer (3)
size_t find_last_of (const char* s, size_t pos, size_t n) const;
character (4)
size_t find_last_of (char c, size_t pos = npos) const;

 

现在输入 symbol(x y what),  希望从末尾开始搜寻`)`, 则建议使用 

size_t rfind (char c, size_t pos = npos) const;

rfind() 会从指定的 pos 参数开始在字符串中往前寻找第一次出现的字符c.

 

参考

cplusplus.com/reference/string/string/find_last_of/

2. 整型变量未定义时, 在 Debug 时显示的值

Posted by haifeng on 2023-08-29 07:56:42 last update 2023-08-29 07:56:42 | Answers (0) | 收藏


当一个整型变量未定义时, 在 Debug 时显示的值为 -858993460 

3. C 语言中打开文件的代码

Posted by haifeng on 2023-03-05 11:27:43 last update 2023-03-05 11:42:17 | Answers (0) | 收藏


 

FILE* inputFile;

const char * file_in;

if ((inputFile = fopen(file_in, "r")) == NULL)     //读取输入文件
{
printf("\nerror: 无法打开输入文件: %s\n\n", file_in);
std::cerr << "Parser: could not open input file '" << file_in << "'!\n";
return EXIT_FAILURE;
}

 

但若在 Visual Studio 下, 会提示 fopen 不安全, 建议使用 fopen_s.

//Declaring a variable of type errno_t to store the return value of fopen_s
    errno_t error_code1; 
    //Opening file in r mode
    error_code1 = fopen_s(&inputFile, file_in, "r");
    if (error_code1 != 0) {
        printf("Error! Failed to open file %s in r mode!", file_in);
    }

5. 检测输入的参数 str 是否是有效的二进制表达式

Posted by haifeng on 2023-02-14 10:49:44 last update 2023-02-14 10:49:44 | Answers (0) | 收藏


//检测 str 是否只包含数字0或1, 也就是检测是否是二进制数
//11010010 

bool BigNumber::isBinaryNumber(const string &str)
{
    return find_if(str.begin(), str.end(),
        [](char c) { 
          return !((c=='0') || (c=='1')) ;
        }) == str.end();
}

 

修改上面的程序, 使满足下面的要求

1111011.01110100101111 也是有效的二进制数. 注意小数点最多只能一个.

6. 整数到字符串的转换

Posted by haifeng on 2022-06-25 12:06:58 last update 2022-06-25 12:53:00 | Answers (0) | 收藏


在非标准 C 库中有一个函数 itoa, 可以将各种进制的整数转换为C风格的字符串.

函数原型为

char *  itoa ( int value, char * str, int base );

 

字符串转为长整型

long int atol ( const char * str );

 

7. 程序设计竞赛网站

Posted by haifeng on 2022-03-30 11:40:45 last update 2022-03-30 11:40:45 | Answers (0) | 收藏


洛谷

https://www.luogu.com.cn/

 

蓝桥杯

https://dasai.lanqiao.cn/

 

ACM/ICPC 信息站
http://acmicpc.info

中国大学生程序设计竞赛
China Collegiate Programming Contest
https://ccpc.io/

8. Compiler Explorer

Posted by haifeng on 2022-03-09 12:31:49 last update 2022-03-09 12:31:49 | Answers (0) | 收藏


Compiler Explorer (godbolt.org)

https://godbolt.org/

 

C++  -->  汇编

9. 文件操作

Posted by haifeng on 2022-02-16 09:06:25 last update 2022-02-16 09:06:25 | Answers (0) | 收藏


现在有扫描后的若干文件, 命名如下.

 

    扫描.pdf
    扫描0001.pdf
    扫描0002.pdf
    扫描0003.pdf
    扫描0004.pdf
    扫描0005.pdf
    扫描0006.pdf
    扫描0007.pdf
    扫描0008.pdf
    ... ... ... ... ...
    扫描0143.pdf
    扫描0144.pdf
    扫描0145.pdf
    扫描0146.pdf
    扫描0147.pdf


但是其中文件名末尾数字凡是奇数的, 都需要进一步操作(旋转180°). 

请将这些文件 "扫描xxxx.pdf" 其中 xxxx 为奇数的移动(或拷贝)到一个文件夹.

 

可以使用任何语言, 例如:  Perl, PHP, Python, C, C++, 批处理等等.

10. 关于 $e$ 的连分数表达式

Posted by haifeng on 2022-02-06 15:48:45 last update 2022-02-06 15:50:33 | Answers (0) | 收藏


证明:

\[
e=3+\frac{-1}{4+\frac{-2}{5+\frac{-3}{6+\frac{-4}{7+\frac{-5}{8+\cdots}}}}}
\]

 

编写程序, 输入 N, 输出形如下面的表达式. 

例如: 当 N=9 时, 输出

3+(-1)/(4+(-2)/(5+(-3)/(6+(-4)/(7+(-5)/(8+(-6)/(9+(-7)/10))))))

即 $3+(-1)/(4+(-2)/(5+(-3)/(6+(-4)/(7+(-5)/(8+(-6)/(9+(-7)/10))))))$

<[1] [2] [3] [4] >