读书人

c++有关问题(多个)

发布时间: 2012-02-19 19:43:38 作者: rapoo

c++问题(多个)?
一、
islower(c)
true if c is a lowercase letter.
isprint(c)
TRue if c is a printable character.
ispunct(c)
TRue if c is a punctuation character.
isspace(c)
true if c is whitespace.
isupper(c)
TRue if c is an uppercase letter.
为什么我在使用上面的函数时不加#include <cctype> 也对,c++ primer说是#include <cctype> 而msdn上说是Header: <locale>


二、
#include <iostream>
#include <vector>
#include <string>
using std::vector;
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main()
{
unsigned a[10];
for(size_t ix=0;ix!=10;++ix) //size_t unsigned integer
a[ix]=ix;
unsigned b[10];
for(size_t ix=0;ix!=10;++ix)
b[ix]=a[ix];
cout < < "A: " < <endl;
for(size_t ix=0;ix!=10;++ix)
{
cout < <a[ix] < < ' ';
}
cout < <endl < < "B: " < <endl;
for(size_t ix=0;ix!=10;++ix)
{
cout < <b[ix] < < ' ';
}
cout < <endl;
return 0;
}
f:\2005\p3\p3\lab5.cpp(13) : warning C4267: '= ' : conversion from 'size_t ' to 'unsigned int ', possible loss of data
//size_t 和unsigned integer是一样的为什么还会提示错误呢


三、
不是说只有string才能直接=吗?
为什么在2005中能通过,而在6.0中就不行呢????????
F:\C++\lab2.cpp(11) : error C2059: syntax error : 'string '
F:\C++\lab2.cpp(12) : error C2059: syntax error : 'string '
在6.0中把char *a( "nihao ");改为char *a= "nihao ";就对了?????
#include <iostream>
#include <vector>
#include <string>
using std::vector;
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main()
{
char *a( "nihao ");
char *b( "nihao ");
if(a==b)
cout < < "equal " < <endl;
else
cout < < "unqual " < <endl;
const int str_size=80;
char str[str_size];
cin> > str;
cout < <str < <endl;
cout < <b < <endl;
return 0;
}


四、
f:\2005\p3\p3\lab14.cpp(14) : warning C4996: 'strcpy ' was declared deprecated
d:\program files\microsoft visual studio 8\vc\include\string.h(73) : see declaration of 'strcpy '
Message: 'This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details. '
f:\2005\p3\p3\lab14.cpp(15) : warning C4996: 'strcat ' was declared deprecated


d:\program files\microsoft visual studio 8\vc\include\string.h(78) : see declaration of 'strcat '
Message: 'This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details. '
Build log was saved at "file://f:\2005\P3\P3\Debug\BuildLog.htm "
lab1 - 0 error(s), 2 warning(s)

#include <iostream>
#include <vector>
#include <string>
using std::vector;
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main()
{
char *a( "zhangshuntong ");
char *b( "nihao ");
char *c=new char[strlen(a)+strlen(b)+1];
strcpy(c,a); //这里为什么会有警告
strcat(c,b);
cout < <c < <endl;
delete [] c;
string d( "zhangshuntong ");
string e( "nihao ");
string f(d+e);
cout < <f < <endl;
return 0;
}

五、
// basic_string_c_str.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
//这是msdn上的一段代码
int main( )
{
using namespace std;

string str1 ( "Hello world " );
cout < < "The original string object str1 is: "
< < str1 < < endl;
cout < < "The length of the string object str1 = "
< < str1.length () < < endl < <str1.size() < < endl;

// Converting a string to an array of characters//这段代码有什么用处,还有data()函数的作用?
const char *ptr1 = 0;
ptr1= str1.data ();
cout < < "The modified string object ptr1 is: " < < ptr1
< < endl;
cout < < "The length of character array str1 = "
< < strlen ( ptr1) < < endl < < endl;

// Converting a string to a C-style string
const char *c_str1 = str1.c_str ( ); //这里是不是建立了一个string字符串的副本


cout < < "The C-style string c_str1 is: " < < c_str1
< < endl;
cout < < "The length of C-style string str1 = "
< < strlen ( c_str1) < < endl < < endl;
}

六、
The LENGTH, SIZE, and TYPE operators have a limited meaning in inline assembly. They cannot be used at all with the DUP operator (because you cannot define data with MASM directives or operators). But you can use them to find the size of C or C++ variables or types:
•The LENGTH operator can return the number of elements in an array. It returns the value 1 for non-array variables.
•The SIZE operator can return the size of a C or C++ variable. A variable 's size is the product of its LENGTH and TYPE.
•The TYPE operator can return the size of a C or C++ type or variable. If the variable is an array, TYPE returns the size of a single element of the array.
For example, if your program has an 8-element int array,
Copy Code

int arr[8];
the following C and assembly expressions yield the size of arr and its elements.
__asm C Size
LENGTH arrsizeof(arr)/sizeof(arr[0])8
SIZE arrsizeof(arr)32
TYPE arrsizeof(arr[0])4
Length和size有什么不同


[解决办法]
include <string> 以后就有这些函数了
[解决办法]
string::sizetype 应该是跟机器有关的吧 只不过这里恰好等于usigned int了
[解决办法]
data()返回对应string对象的一个char数组指针 至于size和length有什么不同 你可以这样试验
int a[4]
std::cout < <sizeof(a) < <std::endl;
[解决办法]
都是include文件的问题


[解决办法]
三 和 四 我编译了 都能通过的
六 length返回的是字符串的长度
sizeof返回的是字符串所占的字节数

读书人网 >C++

热点推荐