| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1544 人关注过本帖
标题:[求助]字符串转为字符数组,可以吗?
只看楼主 加入收藏
_雕刻时光
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2005-11-8
收藏
 问题点数:0 回复次数:4 
[求助]字符串转为字符数组,可以吗?
       一个单词是一个字符串,要操作的是单词中的字母,单词有很多,所以从文件中输入,当然可以一次输进一个字母,遇到空格或其他一些特殊字符时,算是一个单词结束,但为了方便,用while(is >> string)控制输入(is 是已定义的输入流名),一次输进一个单词,请问怎么对这个字符串串中的字符操作,是不是要现将字符串转为字符数组?该怎么样转变?谢谢!

[此贴子已经被作者于2005-11-10 18:18:19编辑过]


搜索更多相关主题的帖子: 字符 单词 定义 字母 
2005-11-10 17:36
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 

以下这个文件为美国AP考试所用的,可以很好地对C++字符串进行处理.


#ifndef _PSTRING_H_
#define _PSTRING_H_

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>


template <class T>
class pstringT
{

public:
pstringT<T>(); //default constructor
pstringT<T>(const pstringT<T> &); //copy constructor
pstringT<T>(const T *copy); //copy constructor from C-style string
pstringT<T>(T ch); //copy constructor from single character
virtual ~pstringT<T>(); //destructor

inline T * c_str() const; //returns a null-terminated, C-style string
inline int length() const; //returns the number of characters in the string

int find(const pstringT<T> &str) const; //return index of str or -1 if not found
int find(const T ch) const; //return index of ch or -1 if not found
pstringT<T> substr(int pos, int len) const; //returns substring from pos of length len

T & operator [] (int n); //access a character (mutable)
const T operator [] (int n) const; //access a character (immutable)

const pstringT<T> & operator = (const pstringT<T> &); //assignment operator
const pstringT<T> & operator = (const T * const); //assignment operator from C-style string
const pstringT<T> & operator = (const T); //assignment operator from single character

const pstringT<T> & operator += (const pstringT<T> &); //concatenation operator
const pstringT<T> & operator += (const T * const); //concatenation operator from C-style string
const pstringT<T> & operator += (const T); //concatenation operator from single character

protected:
T *mystring;

};

//typedef for regular pstrings
typedef pstringT<char> pstring;

//concatenation operators
template <class T> pstringT<T> operator + (const pstringT<T> &, const pstringT<T> &);
template <class T> pstringT<T> operator + (const pstringT<T> &, T);
template <class T> pstringT<T> operator + (T, const pstringT<T> &);
template <class T> pstringT<T> operator + (const pstringT<T> &, const T * const);
template <class T> pstringT<T> operator + (const T * const, const pstringT<T> &);

//stream operators
template <class T> inline ostream & operator << (ostream &, const pstringT<T> &);
template <class T> istream & operator >> (istream &, pstringT<T> &);
template <class T> istream & getline(istream &, pstringT<T> &);

//comparison operators
template <class T> inline bool operator == (const pstringT<T> &, const pstringT<T> &);
template <class T> inline bool operator != (const pstringT<T> &, const pstringT<T> &);
template <class T> inline bool operator < (const pstringT<T> &, const pstringT<T> &);
template <class T> inline bool operator <= (const pstringT<T> &, const pstringT<T> &);
template <class T> inline bool operator > (const pstringT<T> &, const pstringT<T> &);
template <class T> inline bool operator >= (const pstringT<T> &, const pstringT<T> &);


template <class T>
ostream & operator << (ostream &os, const pstringT<T> &out)
{
return os << out.c_str() << flush;
}

template <class T>
istream & operator >> (istream &is, pstringT<T> &in)
{
fflush(stdin);
T input_buffer[4096];
is >> input_buffer;
in = input_buffer;
return is;
}

template <class T>
istream & getline(istream &is, pstringT<T> &to_get)
{
fflush(stdin);
T getline_buffer[4096];
is.getline(getline_buffer, 4095);
to_get = getline_buffer;
return is;
}

template <class T>
pstringT<T>::pstringT()
{
mystring = new T[1];
mystring[0] = 0;
}

template <class T>
pstringT<T>::pstringT(const T *copy)
{
mystring = new T[strlen(copy) + 1]; //allocate memory
strcpy(mystring, copy); //copy string
}

template <class T>
pstringT<T>::pstringT(T ch)
{
mystring = new T[2];
mystring[0] = ch;
mystring[1] = 0;
}

template <class T>
pstringT<T>::pstringT(const pstringT<T> & to_create_from)
{
mystring = new T[to_create_from.length()+1];
strcpy(mystring, to_create_from.c_str()); //copy string
}

template <class T>
pstringT<T>::~pstringT()
{
delete[] mystring;
}

template <class T>
T* pstringT<T>::c_str() const
{
return mystring;
}

template <class T>
int pstringT<T>::length() const
{
return strlen(mystring);
}

template <class T>
int pstringT<T>::find(const pstringT<T> & str) const
{
int i, j, endsearch = length() - str.length() + 1;
for(i = 0; i < endsearch; i++)
{
for(; i < endsearch && mystring[i] != str[0]; i++);
if(i == endsearch)
break;

for(j = 0; j < str.length() && mystring[i+j] == str[j]; j++);
if(j == str.length())
return i;
}
return -1;
}

template <class T>
int pstringT<T>::find(const T ch) const
{
for(int i = 0; i < length(); i++)
if(mystring[i] == ch)
return i;

return -1;
}

template <class T>
pstringT<T> pstringT<T>::substr(int pos, int len) const
{
if(pos < 0 || len < 0 || pos >= length())
{
cerr << "\nError: substring (" << pos << "," << len
<< ") out of bounds for string \"" << mystring << '\"' << endl;
exit(1);
}
if(pos + len > length())
len = length() - pos;
T *result = new T[len + 1];
memcpy(result, mystring + pos, len * sizeof(T));
result[len] = 0;
pstringT<T> to_return(result);
delete[] result;
return to_return;
}

template <class T>
const pstringT<T> & pstringT<T>::operator = (const T * const to_copy)
{
delete[] mystring; //deallocate mem
mystring = new T[strlen(to_copy)+1];
strcpy(mystring, to_copy); //copy string
return *this;
}

template <class T>
const pstringT<T> & pstringT<T>::operator = (const T ch)
{
delete[] mystring; //deallocate memory
mystring = new T[2];
mystring[0] = ch;
mystring[1] = 0;
return *this;
}

template <class T>
const pstringT<T> & pstringT<T>::operator = (const pstringT<T> &copy)
{
return *this = copy.c_str(); //call T pointer copier
}

template <class T>
const pstringT<T> & pstringT<T>::operator += (const T * const to_append)
{
T *newbuffer = new T[length() + strlen(to_append) + 1];
strcpy(newbuffer, mystring);
strcat(newbuffer, to_append);
delete[] mystring;
mystring = newbuffer;
return *this;
}

template <class T>
const pstringT<T> & pstringT<T>::operator += (const pstringT<T> &to_append)
{
return *this += to_append.c_str(); //append T pointer
}

template <class T>
const pstringT<T> & pstringT<T>::operator += (const T to_append)
{
T *newstring = new T[length()+2];
strcpy(newstring, mystring);
delete[] mystring;
mystring = newstring; //points to new string
mystring[length()+1] = 0; //null terminator
mystring[length()] = to_append;
return *this;
}

template <class T>
pstringT<T> operator + (const pstringT<T> & lval, const pstringT<T> & rval)
{
pstringT<T> to_return(lval);
return to_return += rval;
}

template <class T>
pstringT<T> operator + (const pstringT<T> & lval, const T & rval)
{
pstringT<T> to_return(lval);
return to_return += rval;
}

template <class T>
pstringT<T> operator + (T lval, const pstringT<T> & rval)
{
pstringT<T> to_return(lval);
return to_return += rval;
}

template <class T>
pstringT<T> operator + (const pstringT<T> & lval, const T * const rval)
{
pstringT<T> to_return(lval);
return to_return += rval;
}

template <class T>
pstringT<T> operator + (const T * const lval, const pstringT<T> & rval)

{
pstringT<T> to_return(lval);
return to_return += rval;
}

template <class T>
T & pstringT<T>::operator [] (int n)
{
if(n<0 || n>=length())
{
cerr << "\nError: index out of range: " << n << " in string \""
<< mystring << "\" of length " << length() << endl;
exit(1);
}
return mystring[n];
}

template <class T>
const T pstringT<T>::operator [] (int n) const
{
if(n<0 || n>=length())
{
cerr << "\nError: index out of range: " << n << " in string \""
<< mystring << "\" of length " << length() << endl;
exit(1);
}
return mystring[n];
}

template <class T>
bool operator == (const pstringT<T> &lval, const pstringT<T> &rval)
{
return strcmp(lval.c_str(), rval.c_str()) == 0;
}

template <class T>
bool operator != (const pstringT<T> &lval, const pstringT<T> &rval)
{
return strcmp(lval.c_str(),rval.c_str()) != 0;
}

template <class T>
bool operator < (const pstringT<T> &lval, const pstringT<T> &rval)
{
return strcmp(lval.c_str(), rval.c_str()) < 0;
}

template <class T>
bool operator <= (const pstringT<T> &lval, const pstringT<T> &rval)
{
return strcmp(lval.c_str(), rval.c_str()) <= 0;
}

template <class T>
bool operator > (const pstringT<T> &lval, const pstringT<T> &rval)
{
return strcmp(lval.c_str(), rval.c_str()) > 0;
}

template <class T>
bool operator >= (const pstringT<T> &lval, const pstringT<T> &rval)
{
return strcmp(lval.c_str(), rval.c_str()) >= 0;
}

#endif


=×&D o I p R e E n C g T l X&×=
2005-11-10 21:49
_雕刻时光
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2005-11-8
收藏
得分:0 
谢谢 虽然我一时还不明白,但我会慢慢解读的
2005-11-12 19:16
ElfDN
Rank: 4
等 级:贵宾
威 望:11
帖 子:291
专家分:0
注 册:2005-11-13
收藏
得分:0 
//别听2楼的瞎掰,没那么麻烦的
//你定义好string s;就成了
for(string s; cin>>s; cout<<endl){
int len=s.length();
for(int i=0; i<len; i++)
cout<<s[i]<<" ";
}
//string是个流,本身就可以当数组处理的

2005-11-17 10:47
czk037
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-11-26
收藏
得分:0 
4楼的循环可以用吗?还是不会
2005-11-26 10:29
快速回复:[求助]字符串转为字符数组,可以吗?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041699 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved