如何将一个数组转为字符串
怎么将一个数组转为字符串
rt,每个数之间用特殊符号分隔。接收方再将字符串还原为数组,这个应该怎么弄??
------解决方案--------------------
刚写的,intArray是输出
rt,每个数之间用特殊符号分隔。接收方再将字符串还原为数组,这个应该怎么弄??
------解决方案--------------------
刚写的,intArray是输出
- C/C++ code
typedef basic_string<char>::size_type S_T; std::vector<int> m_list; int intArray[1000]; int intCount; int AddToArray(const char * inputString,const char split) { S_T ulStartIndex = 0; S_T ulFindIndex = 0; S_T len = 0; bool bGo = true; string strInpuString = inputString; do { ulFindIndex = strInpuString.find(split,ulStartIndex); if(ulFindIndex == string::npos) { bGo = false; } len = ulFindIndex - ulStartIndex; if(len < 0) { cout<<"ulFindIndex is error, please check out\n"<<endl; return -1; } m_list.push_back(atoi(strInpuString.substr(ulStartIndex,len).c_str())); ulStartIndex = ulFindIndex+1; } while (bGo); intCount = m_list.size(); int i=0; vector<int>::iterator myiter = m_list.begin(); for(i=0;i<intCount;i++) { intArray[i] = *myiter; myiter++; } return 0; }
------解决方案--------------------
CString::Tokenize()以分号作为分隔符来拆分字符串
或者使用_tcstok()
或者使用AfxExtractSubString
------解决方案--------------------
- C/C++ code
#include <sstream> #include <string> #include <vector> std::string ToString(int* pArr, int n) { std::ostringstream stream; for (int i=0; i<n; i++) stream<<pArr[i]<<","; //删除最后面的 ',' stream.seekp(-1, std::ios_base::end); stream.write("", 1); return stream.str(); } void ToInt(const std::string& str, std::vector<int>& buf) { std::istringstream stream(str); int a; while (!(stream>>a).eof()) { buf.push_back(a); stream.ignore(1); //跳过 ',' } } int _tmain(int argc, _TCHAR* argv[]) { //数组 int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; std::string str = ToString(arr, 10); printf("%s\n", str.c_str()); std::vector<int> v; ToInt(str, v); return 0; }
------解决方案--------------------
------解决方案--------------------
使用ssacnf()函数也是可以的