小编πάν*_*ῥεῖ的帖子

C++程序添加两个整数

以下是使用用户定义的函数添加两个整数的两种方法但两者都不同,因为一个使用int类型函数而另一个使用void类型函数但在这两种情况下我得到相同的输出所以我真的很混淆哪一个选择.所以请告诉我这些和选择哪一个之间的区别.提前致谢

使用int类型函数(用户定义函数):

#include <iostream>
using namespace std;
int add(int, int);
int main()
{
    int a = 5, b = 6, sum;
    sum = add(a, b);
    cout << sum;
    return 0;
}
int add(int x, int y)
{
    int add;
    add = x + y;
    return add;
}
Run Code Online (Sandbox Code Playgroud)

使用void类型(用户定义的函数):

#include <iostream>
using namespace std;
void add(int, int);
int main()
{
    int a = 5, b = 6;
    add(a, b);
    return 0;
}
void add(int x, int y)
{
    int add;
    add …
Run Code Online (Sandbox Code Playgroud)

c++

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

C++结合了3个整数

我需要从一些Area eq加入3个整数

int sens1[]= {11,22,13,66,2};

int x= ?? // and here i need to join sens1[0], sens1[1] and sens1[2] 
          // so the X to be=112213 not like char, like integer value
Run Code Online (Sandbox Code Playgroud)

c++ arrays integer join concatenation

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

如何检查矢量元素是否连续排序?

我需要检查在我的向量中元素是否连续排序?

for(i=1; i<=K; i++)
    if(v[i]=v[i+1]-1)
Run Code Online (Sandbox Code Playgroud)

如果该语句为真,我想返回最大整数.

恩.4 5 6 7

7
Run Code Online (Sandbox Code Playgroud)

c++ vector

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

如何做这个c ++循环

对不起,我是编程方面的新手.我想请求帮助,我想显示从200到400的范围编号,但它不应显示数字250.

这就是我所做的.

int main () {
     for (int i=200; i<=400; i++) {
         std::cout << "value of i: " << i << endl;  
     }

     return 0;
 }
Run Code Online (Sandbox Code Playgroud)

我很成功地显示了范围号码,但它显示了所有号码.

c++ loops for-loop

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

为什么结果如此不同?

我不知道为什么类似的代码有很大的不同?第一个代码正常输出,但第二个代码输出一些无法识别的字符.谁能为我解释一下?

THKS

#include <iostream>

using namespace std;

int main(){

    char a[5] = { 'A', 'B', 'C', 'D' };

    cout << a + 1 << endl;

    char b[5] = {'a','b','c','d','e'};
    cout << b+1 << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

-6
推荐指数
1
解决办法
64
查看次数

c ++在二维地图中找到一个关键匹配(地图图)

我有以下地图用于存储客户名称和使用其代码和描述选择的itens字典,例如:

Name         Item Code, Item Description
Customer1 -> 1234,      Item 1
             1233,      Item 2
             1232,      Item 3
Run Code Online (Sandbox Code Playgroud)

所以我使用以下结构:

std::map<std::string, std::map<std::string, std::string>> purchase_list;
Run Code Online (Sandbox Code Playgroud)

现在我需要查找现有名称和现有项目代码.

要查找现有名称:

if (purchase_list.find(purchase_list.begin(), purchase_list.end(), "SearchName") != purchase_list.end())
   std::cout << "SeachName found!!" << std::endl;
Run Code Online (Sandbox Code Playgroud)

对我来说这应该工作,但我在编译时遇到以下错误:

no matching function for call to ‘std::map<std::basic_string<char>, std::map<std::basic_string<char>, std::basic_string<char> > >::find(std::map<std::basic_string<char>, std::map<std::basic_string<char>, std::basic_string<char> > >::iterator, std::map<std::basic_string<char>, std::map<std::basic_string<char>, std::basic_string<char> > >::iterator, std::string&)’ search_name) == purchase_list.end())
Run Code Online (Sandbox Code Playgroud)

我要改变"SearchName"格式吗?这里的语法是什么?

要搜索商品代码,我会重复相同的代码,寻找客户找到的条目...

谢谢你的帮助......

c++ stdmap

-6
推荐指数
1
解决办法
67
查看次数

将 C++ 程序连接到数据库 - 一种独特的方式

好的,所以我有这个由 postgreSQL 制作的数据库,我想知道我是否以及如何连接我的数据库和我的 C++ 程序。我想将 .exe 文件发送给我的朋友,然后每次他打开它时,它都会将他的一些信息以 PK(主键)id 发送到我的数据库,然后我的程序“窃取”所有信息,例如 IP 和一些东西。

所有内容仅用于教育目的!我有 postgreSQL 技能和 C++ 技能,但我从来没有想过,我怎么能做到这一点。我在互联网上搜索了几个小时,没有合理的帮助,我想要这样的例子。

c++ sql database postgresql

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

std :: remove_if GCC实现效率不高?

另一个问题来看,似乎有证据表明,std::remove_if与以下实施相比,海湾合作委员会的实施并不能提供同等效率:

'raw homebrew'解决方案:

static char str1[100] = "str,, ing";
size_t size = sizeof(str1)/sizeof(str1[0]);

int bad = 0;
int cur = 0;
while (str1[cur] != '\0') {
    if (bad < cur && !ispunct(str1[cur]) && !isspace(str1[cur])) {
        str1[bad] = str1[cur];
    }
    if (ispunct(str1[cur]) || isspace(str1[cur])) {
        cur++;
    } else {
        cur++;
        bad++;
    }
}
str1[bad] = '\0';
Run Code Online (Sandbox Code Playgroud)

时序输出:

0.106860

示例基准测试代码用于std::remove_if对同一问题的解决方案:

bool is_char_category_in_question(const char& c) {
    return std::ispunct(c) || std::isspace(c);
}

std::remove_if(&str1[0], &str1[size-1], is_char_category_in_question);
Run Code Online (Sandbox Code Playgroud)

时序输出: …

c++ gcc

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

函数初始化C中的变量

我应该使用一个函数来为变量赋值初始值,但是不明白为什么我的代码中的init函数没有初始化a,b,c,它给出了一些垃圾数.此外,我应该使用旋转函数使用变量a,b,c的静态声明,但由于它的静态它不交换变量.我怎么做的?

 void init(int num1, int num2, int num3);

 int main(void){

 int a, b, c;



 init(a, b, c); 


 printf("a=%d, b=%d,  c=%d\n", a, b, c);


rotate();

 return 0;
 }

 void init(int num1, int num2, int num3){

 printf("Enter value for num1: ");
 scanf("%d",&num1);
 printf("Enter values for num2: ");
 scanf("%d",&num2);
 printf("Enter values for num3: ");
 scanf("%d",&num3);
 }


void rotate(void){
static int a,b,c;
int temp;
temp=a;
a=b;
b=c;
c=temp;}
Run Code Online (Sandbox Code Playgroud)

c

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

标签 统计

c++ ×8

arrays ×1

c ×1

concatenation ×1

database ×1

for-loop ×1

gcc ×1

integer ×1

join ×1

loops ×1

postgresql ×1

sql ×1

stdmap ×1

vector ×1