如何在c语言中使用STL库 [英] how to use STL library in c language

查看:674
本文介绍了如何在c语言中使用STL库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我在我的c ++代码中使用此代码,现在我想使用相同的

代码进行交流语言项目...

有人请指导我该怎么办?


====================== ============================ =============

#包括stdafx.h

#include< vector // for vector STL


using namespace std; //必须


//用于测试

struct MODEM

{

int index;

int snumber;

char flag;

};


int main(int argc,char * argv [])

{

vector< MODEM * v; //向量在STL中定义(作为动态

数组)


//用于调制解调器结构的mem分配

MODEM * test =(MODEM *)malloc(sizeof(MODEM));


//测试值

test-> index = 1;

test-> snumber = 5556;

test-> flag =''p'';


//向量数组动态增长(索引0)

v.push_back(测试);

//现在取回第二个调制解调器值

MODEM * temp = v [ 0];


printf(" MODEM [1]索引值%d \ n",temp-> index);

printf(" ; MODEM [1]序列号值%d \ n",temp-> snumber);

printf(" MODEM [1]标志值%c\ n,temp-> ; flag);

返回0;

}

================== ================================ ===========

等待紧急回复....

Dear All,
I am using this code in my c++ code and now i want to use this same
code for a c language project...
would some one please guide me what to do ??

================================================== =============
#include "stdafx.h"
#include <vector // for vector STL

using namespace std; // required

// for testing
struct MODEM
{
int index;
int snumber;
char flag;
};

int main(int argc, char* argv[])
{
vector<MODEM*v; // vector is defined in STL (working as dynamic
array)

// mem allocate for modem structure
MODEM* test = (MODEM*)malloc(sizeof(MODEM));

// test values
test->index = 1;
test->snumber = 5556;
test->flag = ''p'';

// vector array dynamically grow (index 0)
v.push_back(test);
// now get back the second modem values
MODEM* temp = v[0];

printf("MODEM[1] Index Value %d\n", temp->index);
printf("MODEM[1] Serial Number Value %d\n", temp->snumber);
printf("MODEM[1] Flag Value %c\n", temp->flag);
return 0;
}
================================================== ===========

waiting for an urgent response ....

推荐答案

um ****** @ gmail.com 写道:

亲爱的所有人,

我是在我的c ++代码中使用此代码,现在我想使用相同的

代码进行交流语言项目...

请某人请指导我做什么?
Dear All,
I am using this code in my c++ code and now i want to use this same
code for a c language project...
would some one please guide me what to do ??



学习C.为什么你认为C ++的混淆应该是C的一部分是

之谜。

没有stdafx.h在C和C ++中,虽然你的实现可以提供一个,所以任何与stdafx.h相关的东西都可以。属于

特定于实现的新闻组,而不是C或C ++的新闻组。


C没有STL,< vector>," using"语句(BTW,*不*需要

用于你的C ++程序,无论你在评论中写什么),没有

类型的名字如foo< bar (所以显然没有成员索引,snumber,

或flag),并且没有名为v.push_back的函数。


转换malloc的返回值是坏的样式在C;使用malloc
没有声明的
是一种罪,即使在C89中它也会被错误地解释为返回int。

Learn C. Why you think the obfuscations of C++ should be part of C is a
mystery.

There is no "stdafx.h" in either C nor C++, although your implementation
may provide one, so anything related to "stdafx.h" belongs in an
implementation-specific newsgroup, not one for C or C++.

C has no STL, <vector>, "using" statement (which is, BTW, *not* required
for your C++ program, no matter what you write in your comments), no
types with names like foo<bar(so obviously no members index, snumber,
or flag), and no functions named v.push_back.

Casting the return value of malloc is bad style in C; using malloc
without a declaration is a sin, even in C89 where it would be
interpreted falsely as returning an int.


um******@gmail.com 写道:

亲爱的所有,

我在我的c ++代码中使用此代码,现在我想使用相同的

代码进行交流语言项目.. 。
Dear All,
I am using this code in my c++ code and now i want to use this same
code for a c language project...



看起来像杂交的混搭。


只需使用链接列表,你可以使用vector。 />

-

Ian Collins。

Looks like a mish mash of a hybrid.

Just use a linked list where you use vector.

--
Ian Collins.


um ****** @ gmail.com 说:

亲爱的所有人,

我在我的c ++代码中使用此代码,现在我想使用相同的

代码进行交流语言项目...

请一些人请指导我一下去做 ??
Dear All,
I am using this code in my c++ code and now i want to use this same
code for a c language project...
would some one please guide me what to do ??



第1步:扔掉你的代码。

第2步:学习C ++,这样你才明白为什么stdafx.h不是

语言的一部分,但是你设计糟糕的IDE有一部分污染已经被你的程序所淹没,但很容易被删除。处理

这种删除的后果在这里是偏离主题的,但这很容易

也可以。

步骤3 :学习C.

第4步:在C中正确编写代码。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

Step 1: throw away your code.
Step 2: learn C++, so that you understand why "stdafx.h" is not part of the
language, but part of the pollution that your badly-designed IDE has
crufted into your program, but which can easily be removed. Dealing with
the consequences of that removal is off-topic here, but that''s pretty easy
to do too.
Step 3: learn C.
Step 4: write your code properly in C.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


这篇关于如何在c语言中使用STL库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆