Hello!

I have a string which may look like this:
(arne kristoffer)(1231232)(12.12.12)(asdasdf 12)

This is just an silly example, but never mind.

THe point is that i need to seperate the contents of the string into a string vector. This is my code (which of course doesn't work, if it had, I wouldn't have been posting here...):

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main ()
{
	vector<string> vectorOfTags;
	vectorOfTags.push_back("");
	vectorOfTags.push_back("");
	vectorOfTags.push_back("");
	typedef vector<string>::size_type vecsz;
	vecsz size = vectorOfTags.size();
	int position = 0;
	string *PointerToElement = &vectorOfTags[position];

	string str("(56465)(Arne Kristoffer)");
  	for (int i=0; i < str.length(); i++)
    {
		if (str[i] != '(' || str[i] != ')')
			*PointerToElement +=str[i];
		else if (str[i] == ')')
			position++;
    }
	cout << vectorOfTags[0] << endl;

    cin.get();
    return 0;
}

The result of this code, is that str is written to *PointerToELement without changing the position-variable, so str == VectorOfTags[0].

How could I change the code, so it would work? Please change my code instead of making an new one, since the reason why I do this is to learn, not only to make this program the fastest way posible.

int position = 0;
string *PointerToElement = &vectorOfTags[position];

is *PointerToElement = &vectorOfTags[0]; and never change

else if (str[i] == ')')
position++;

local change only

Okay, thanks for answering, but how can i make the *PointerTOElement to point one element higher then?

;)

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
	vector<string> vectorOfTags;
	vectorOfTags.push_back("");
	vectorOfTags.push_back("");
	vectorOfTags.push_back("");
	int position = 0;

	string str("(56465)(Arne Kristoffer)");
	for (size_t i=0; i < str.length(); i++) {
		if (str[i] != '(' || str[i] != ')') {
			vectorOfTags[position] +=str[i];
			if (str[i] == ')')
				position++;
		}
	}
	cout << vectorOfTags[0] << endl;

	cin.get();
	return 0;
}

Why is the if (str == ')') inside if (str != '(' || str != ')')?

A ')' won't slip through to the second if.

Why is the if (str == ')') inside if (str != '(' || str != ')')?

A ')' won't slip through to the second if.

and why it won't?

and why it won't?

:)
When you ask in that way, I'm sure you're right, hehe, but I thought the first IF would "refuse" an ')' to continue.

Let's analyze this line:
if (str != '(' || str != ')')
Start with this:
str != '('
It will be true unless str == '('.

A similar statement can be made about this:
str != ')'

But if stri == '(' then it can't be ')' and visa versa.

That means the compound statement will always have at least one true value on either side of the || operator meaning the compound statement will always be true. If you want every char of the original string to be added to the new string then why bother testing it's value in the first place?

What I suspect you probably want is to extract all char out of the original string, str, and analyze them one by one. You probably want to store them in vectorOfStrings[j] if they aren't ()s and use the ) to terminate any given vectorOfStrings[j] and start the next vectorOfStrings[j], if there is one.

If that's true, then you could use the && operator instead of the || operator.


if ((str[i] != '(' ) && (str[i] != ')'))
  vectorOfStrings[j] += str[i];
else if((str[i] == ')')
  ++j;

EDIT: I apologize to anyone who viewed the original post for my extensive edit. I realized that concatenation was being used rather than assignment all along. Sorry.

Thank you very much for your help! :)

EDIT: Do anyone know how to delete the first and last character from a string?

What do you mean delete and what do you mean by string?

std::strings have an erase() method.

C style strings can use a loop to assign the char at index i + 1 to the char at index i to get rid of the first char. To get rid of the last char you can determine the length of the string with strlen(). Then assign the char at index of length to the char at the index of length - 1.

This question has already been answered. Start a new discussion instead.