您的位置:首页 > 产品设计 > UI/UE

有一个很帅的方法,可惜我对于string的原理还是不太懂 另外unique 和sort 可以处理string呢 嗯,解决了,但还可以优化哦

2017-11-20 20:30 483 查看
题目:点击打开链接
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdlib.h>
using namespace std;
string a[10000];

int main()
{
int i;
char str[10000];
gets(str);
char *p = strtok (str," ");
while (p != NULL){
cout << p << endl;
i = 0;
//现在我已知的是一个首地址,而我要把一个地址赋给一个我连原理都看不懂的string你在逗我呢,
//等我弄清原理再来
i ++;
p = strtok(NULL," ");
cout << a[i-1] << endl;
}
int k;
for (k = 0;k < i;k ++)
cout << a[k] << endl;
sort (a,a+i);
int j = unique(a,a+i) - a;
cout << j;
}
这里的问题主要就是无法把指针的值赋值给字符串,但是我现在好像有了一些新的感悟了。



哈哈我成功了#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdlib.h>
#include <set>
using namespace std;
struct Worddd{
char words[100];
};

struct Rule{
bool operator()(const Worddd &s1,const Worddd &s2){
return (strcmp(s1.words,s2.words)<0);
}
};

int main()
{
char str[1000000];
gets(str);
set<struct Worddd,Rule>st;
while (str[0]!='#'){
char*p = strtok(str," ");
int i = 0;
struct Worddd temp;
while (p != NULL){
strcpy(temp.words,p);
st.insert(temp);
p = strtok(NULL," ");
}
int j = st.size();
printf("%d\n",j);
st.clear();
gets(str);
}
}这里我比较骄傲的就是,set 的使用,和结构体的帮大忙,和那个能够把指针(迭代器)转换成字符的大神了。
那么问题来了,结构体到底帮了什么忙呢。
结构体在很多想用[]很纠结的地方可以不用纠结,像什么比较,都可以。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐